开发者

Run two queries on the same table at once?

开发者 https://www.devze.com 2023-02-12 05:42 出处:网络
I need to retrieve distinct records from a column in a mysql table and then retrieve data from each row that the distinct records show up in. Sorry if this isn\'t very clear. I can elaborate if necess

I need to retrieve distinct records from a column in a mysql table and then retrieve data from each row that the distinct records show up in. Sorry if this isn't very clear. I can elaborate if necessary.

EDIT:

+------+------+-----+
|  ip  | hits | day |
+------+------+-----+
| ip1  | 23   | 52  |
+------+------+-----+
| ip2  | 28   | 50  |
+------+------+-----+
| ip1  | 46   | 54  |
+------+------+-----+
| ip4  | 15   | 55  |
+------+------+-----+
| ip2  | 12   | 52  |
+------+-开发者_JAVA百科-----+-----+

I need to select distinct ips, then for each ip add up all the hits for that ip.


  SELECT SUM(`hits`) AS `total_hits`, `ip`
    FROM `ips`
GROUP BY `ip`


It doesn't sound like you need two queries, you just need to select distinct rows for the first column and then use a where clause for the additional data.

0

精彩评论

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