douyueju2912 2013-02-19 12:28
浏览 37

加入两个表,以便我可以使用ORDER BY

I'm pretty new to SQL but hit a wall last night when I was experimenting on my website. I've been trying to figure it out now for hours with no luck, so here's my first post on Stack Overflow!

My issue is that I'm trying to sort a table of results, however what I need to sort by is from a different query from the main data. So I have a table of data that outputs fine, but I can't use ORDER BY to the field I want because it comes from a different query.

I have two tables:

character_
quest_globals

Data I need from the two tables:

character_.id
character_.name
character_.class
character_.level

quest_globals.charid   (Same value as character_.id)
quest_globals.name
quest_globals.value

Ordered by: quest_globals.value

I would like to join these two tables so I can order the results how I like. Do I need to use UNION or JOIN for this?

character_ and quest_globals have a common field as character_.id is the same as quest_globals.charid so I'm guessing I use this to join?

  • 写回答

3条回答 默认 最新

  • dongxian3852 2013-02-19 12:31
    关注
    SELECT c.id, c.name, c.class, c.level, q.name, q.value 
    FROM character_ c
    LEFT JOIN
      quest_globals q
    ON 
      q.charid  = c.id 
    ORDER BY q.value
    

    Also, you can give names to your columns like:

    c.name as char_name, q.name as quest_name
    

    so, it won't be so messy

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了