Repeat occurrences in MySQL

September 24, 2024

Finding repeat occurrences in MySQL

Found myself diagnosing an issue today and my debugging journey led me to needing to determine how many records in a table shared the same value for a specific column. Figured this was something I could pretty easily find out with a well written query. After a little bit of tinkering, here is the gist of that query.

SELECT <target_column>,
       count(*) AS times_used,
FROM <target_table>
GROUP BY <target_column>
HAVING times_used > 1
ORDER BY times_used DESC;

This would ultimately give me something that resembled the following

target_column times_used
123.png 40
abc.png 30
xyz.jpg 20

© 2024 Terrence Eisenhower. All rights reserved.