I would like to determine two things from a single query:
- Most prevalent column in a table
- The amount of times such column was located upon querying the table
Example Table:
user_id some_field 1 data 2 data 1 data
The above would return user_id # 1 as being the most prevalent in the table, and it would return (2) for the total amount of times that it was located in the table.
I have done my research and I came across two types of queries.
- GROUP BY user_id ORDER BY COUNT(*) DESC
- SUM
The problem is that I can’t figure out how to use these two queries in conjunction with one another. For example, consider the following query which successfully returns the most prevalent column.
$top_user = "SELECT user_id FROM table_name GROUP BY user_id ORDER BY COUNT(*) DESC";
The above query returns “1” based on the example table shown above. Now, I would like to be able to return “2” for the total amount of times the user_id (1) was found in the table.
Is this by any chance possible?
Thanks,
Evan
You can include
count(*)in the SELECT list:If you want only the first one: