好的,我有三(3)张表,我想加入
row_id
autoincremented row_id
row_id
我想加入所有这些表,以便我可以查看所有详细信息以及tableC中row_id
和avg评级的实例数。
SELECT *
FROM `tableA` A
LEFT JOIN `tableB` B
ON A.`row_id` = B.`row_id`
LEFT JOIN (
SELECT COUNT( 1 ) AS 'count', Avg(`row_rating`) AS 'avg'
FROM `tableC`
GROUP BY tableC.`row_id`
)C
ON C.`row_id` = A.`row_id`
ORDER BY C.`avg` ASC
此查询的结果合并了所有正确但相同的计数,avg显示在所有行中。
答案 0 :(得分:1)
您希望在内部查询中按row_id
对记录进行分组。在这种情况下,您需要SELECT
row_id
而不是COUNT(1)
,请尝试以下操作:
SELECT *
FROM `tableA` A
LEFT JOIN `tableB` B
ON A.`row_id` = B.`row_id`
LEFT JOIN (
SELECT row_id, Avg(`row_rating`) AS 'avg'
FROM `tableC`
GROUP BY tableC.`row_id`
)C
ON C.`row_id` = A.`row_id`
ORDER BY C.`avg` ASC