连接表上的ORDER BY

时间:2016-04-22 11:35:52

标签: php mysql sql

我正在尝试连接两个表,并且还按照我正在加入的表中的一个列来排序查询结果。一切都很有效,直到我添加select date, max(case when id = 'A' then result end) as a, max(case when id = 'B' then result end) as b, max(case when id = 'C' then result end) as c from t group by date order by date; ,此时我收到此错误:

ORDER BY cm.num

以下是我的完整查询:

Call to a member function fetch_assoc() on boolean

有人有任何建议吗?

修改

所要求的完整代码:

SELECT * 
FROM course 
   JOIN cm ON (course = cm.course) 
WHERE title LIKE '%$searchTerm%' 
LIMIT $limit 
ORDER BY cm.num

4 个答案:

答案 0 :(得分:4)

在订单后面加上限制并专门指定列

即。 JOIN cm ON (course.course = cm.course)

SELECT * 
FROM course 
   JOIN cm ON (course.course = cm.course) 
WHERE title LIKE '%$searchTerm%' 
ORDER BY cm.num
LIMIT $limit 

答案 1 :(得分:1)

您的代码中有(course.itsColumn = cm.course)。当然是一个表,你想加入两列。所以它应该是var path = window.location.pathname; path = path.substr( 0, path.lastIndexOf("/")+1 ); //need to remove 'index.html' from the end of pathname var aud = path+'assets/audio/'; var ogg = aud + 'ogg/' + key + ".ogg"; //works! var snd = new Media(ogg); snd.play();

如果有帮助,请告诉我!

答案 2 :(得分:0)

尝试添加别名。建议通过别名引用连接中的每个表。

SELECT * 
FROM course AS c
   LEFT JOIN cm AS cm ON (c.course = cm.course) 
WHERE title LIKE '%$searchTerm%' 
LIMIT $limit 
ORDER BY cm.num

答案 3 :(得分:0)

您在代码中使用自联接。那么请您查看以下代码:

SELECT * FROM course c 
JOIN course cm ON (c.course = cm.course) 
WHERE c.title LIKE '%$searchTerm%' 
ORDER BY cm.num LIMIT $limit