简体   繁体   中英

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 col2 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM