SQL JOIN变量不起作用

时间:2013-12-03 17:08:39

标签: mysql join left-join

我正在尝试使用2个左连接从3个表中选择变量。

SELECT a.username, a.id, COUNT(c.featured)
FROM user a
LEFT JOIN board b
ON a.id = b.user_id
LEFT JOIN pins c
ON a.id = c.user_id
WHERE c.featured='yes'
GROUP BY b.board_name
ORDER BY COUNT(c.featured) ASC
LIMIT 3

这应该从数据库获得3个最高得分板,并根据它们按升序排列的次数进行排列。我用这个来回应:

".$info['b.board_name']."

然而没有显示

1 个答案:

答案 0 :(得分:0)

我不确定您的b.boardname中是否需要a.username, a.idSELECT,但您必须相应地执行GROUP BY

尝试做:

SELECT a.username, a.id, COUNT(c.featured)
FROM user a
LEFT JOIN board b ON a.id = b.user_id
LEFT JOIN pins c ON a.id = c.user_id
WHERE c.featured = 'yes'
GROUP BY a.username, a.id
ORDER BY COUNT(c.featured) DESC LIMIT 3

OR

SELECT b.board_name, COUNT(c.featured)
FROM user a
LEFT JOIN board b ON a.id = b.user_id
LEFT JOIN pins c ON a.id = c.user_id
WHERE c.featured = 'yes'
GROUP BY b.board_name
ORDER BY COUNT(c.featured) DESC LIMIT 3