我需要对行的列source
进行排序。但是,这是我要查询的表(简化):
+---+-------+-----+
|id |source |foo |
+---+-------+-----+
|1 |5 |hey |
|2 |7 |yo |
+---+-------+-----+
以下是来源:
+---+-------+
|id |name |
+---+-------+
|5 |First |
|7 |Awesome|
+---+-------+
现在,如果我使用这样的查询:
SELECT * FROM table ORDER BY source
...结果将按来源ID而不是其实际名称排序。我可以简单地在PHP中排序结果,但是我正在寻找一个sql-solution(如果可用)。
任何帮助将不胜感激!
最佳答案
听起来您只需要将表连接在一起,就像这样:
SELECT * FROM table, sources WHERE table.source = sources.id ORDER BY sources.name
假设您的来源表称为来源...