I have a rather complex statement that I'm trying to write. I'm not sure how to do it though, or even if it's possible.
I have a logging system that scans a server and catalogs every user that's online at that given moment. Here is how my table looks like:
-----------------
| ab_logs |
-----------------
| id |
| scan_id |
| found_user |
-----------------
id
is an autoincrementing primary key. Has no real value other than that.
scan_id
is an integer that is incremented after each successful scan of all users. It so I can separate results from different scans.
found_user
. Stores which user was found online during the scan.
The above will generate a table that could look like this:
id | scan_id | found_user
----------------------------
1 | 1 | Nick
2 | 2 | Nick
3 | 2 | John
4 | 3 | John
So on the first scan the system found only Nick online. On the 2nd it found both Nick and John. On the 3rd only John was still online.
I know I can get the number of users that were online during a given scan by the following query:
SELECT COUNT(*) FROM ab_logs WHERE scan_id = $scan_id
My problem is that I want to get the above for all scans in one result . Is this possible without looping the above query X times?
您可以使用GROUP BY
函数,如下所示:
SELECT COUNT(*) FROM ab_logs GROUP BY scan_id
是的,您可以像这样使用GROUP BY
:
SELECT COUNT(*) FROM ab_logs GROUP BY scan_id
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.