Arinue74
Arinue74

Reputation: 85

Get records with max time inserted MySQL results

I have some data row in my table.

I want to select the Age which occurs the most.

Person | Group | Age
---
Bob  | 1     | 32  
Jill | 1     | 32  
Shawn| 1     | 42  
Jake | 2     | 29  
Paul | 2     | 36  
Laura| 2     | 39

Desired set:

The age that appears the most is 32.

Upvotes: 0

Views: 75

Answers (4)

Strawberry
Strawberry

Reputation: 33945

SELECT age FROM my_table GROUP BY age ORDER BY COUNT(*) DESC LIMIT 1;

Fairly obviously, in the case of a tie, this result will be misleading.

Upvotes: 0

Tharif
Tharif

Reputation: 13971

After grouping the values you can select the top 1 as below : This would select 32 as answer

SELECT        TOP (1) Age
FROM            tablename
GROUP BY age
ORDER BY COUNT(*) DESC

Upvotes: 0

Santosh Jagtap
Santosh Jagtap

Reputation: 1075

Use the following query

select Person, count(*) as c FROM tableName GROUP BY Age

You can add the limit 1 to get the only only record and order by to get the maximum or minimum age. Use following query

select Person, count(*) as c,Age FROM profile GROUP BY Age ORDER BY c desc LIMIT 0,1

Upvotes: 1

Asim Shahzad
Asim Shahzad

Reputation: 1609

Try something like this

SELECT Person,Group,Age,MAX(field_name)  
FROM table_name;

Upvotes: 0

Related Questions