When I was querying a table by a specific id I would also count how many rows in that table shared the same name:
SELECT *, COUNT(name) FROM table WHERE id = 24601
Now I'm no longer querying a table by a specific id, instead getting all of the table's output. Is there a way I could still get that count for each row?
SELECT *, COUNT(this specific row's name) FROM table
If you want to get the count of rows per name
, then use GROUP BY
:
SELECT name, COUNT(*) AS count
FROM table
GROUP BY name
You'll get one row per distinct value of name
, with a second column that has the integer count.
If you also need the other columns for each row, you should write that as a separate query. Don't combine aggregated results with non-aggregated results in the same query.
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.