table1 table2
id name logo id name value
1 a asd 1 a 2
2 b asd 2 a 3
3 c asd 3 b 4
查询:
SELECT table1.id,table1.logo,avg(table2.value) as average,
count(table2.id) as no FROM table1 INNER JOIN table2 ON table1.name = table2.name
ORDER BY no
此代码只提供一个结果而不是2个结果
答案 0 :(得分:1)
因为您没有GROUP BY
子句,请尝试添加一个。
SELECT a.name,
a.logo,
AVG(b.value) avg_val,
COUNT(*) no
FROM table1 a
INNER JOIN table2 b
ON a.name = b.name
GROUP BY a.Name
ORDER BY no
答案 1 :(得分:0)
FROM REFERENCE
如果在包含no的语句中使用组函数 GROUP BY子句,相当于对所有行进行分组。
所以使用分组
SELECT table1.id,table1.logo,avg(table2.value) as average,
count(table2.id) as no
FROM table1 INNER JOIN table2
ON table1.name = table2.name
GROUP BY table1.name
ORDER BY no
你应该没问题