开发者

MYSQL - Using AVG() and DISTINCT together

How can you write the following in MYSQL?

SELECT AVG(col1) FROM table WHERE DISTINCT col2

more info:

table

col1 | col2
-----------
2    | 555.555.555.555
5    | 555.555.555.555
4    | 444.444.444.444

returns '3'

Basically I'm trying to select average value of col1 where ip addresses in 开发者_StackOverflowcol2 are distinct.


  SELECT col2, 
         AVG(col1) 
    FROM table 
GROUP BY col2


Right, because the distinct clause would find the first and third rows, the average of 2 and 4 is 3.

What I think you're looking for is "group by col2" instead of distinct.


I think you want the group by operator. It will group the rows before running calculations on them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜