简体   繁体   中英

mysql count results with an identical field

So I'm fairly new to web development and I haven't really used php and mysql much before.

I was wanting to query my database to find out how many results had the same value for a certain field. I was thinking along the following lines:

SELECT value_a, COUNT(*)
FROM table_a
WHERE value_a = (SELECT DISTINCT value_a FROM table_a)

but obviously I can't have multiple values from my inner query. How could I do something like this?

Simply do -

IF you want to check a single value -

SELECT COUNT(*) value_a_count
FROM table_a
WHERE value_a = 'value_to_check'
GROUP BY value_a

Or for every values -

SELECT value_a, COUNT(*) value_a_count
FROM table_a
GROUP BY value_a

put your result(check_value) in this query

SELECT COUNT(*) count_value
FROM table_a
WHERE value_a IN (check_value)
GROUP BY value_a

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