Sam
Sam

Reputation: 313

Run two queries on the same table at once?

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  |
+------+------+-----+

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

Upvotes: 1

Views: 253

Answers (2)

alex
alex

Reputation: 490253

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

Upvotes: 2

The Muffin Man
The Muffin Man

Reputation: 20004

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.

Upvotes: 0

Related Questions