0
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 个结果

4

2 回答 2

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
于 2013-06-25T03:57:56.200 回答
0

参考

如果在不包含 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

你应该没事

查看 SQL 小提琴

于 2013-06-25T04:11:10.353 回答