Manish Pradhan
Manish Pradhan

Reputation: 1188

Simple Count Mysql query

receipt_id           order_id
1                       A
2                       B
3                       B
4                       C
5                       C

What is the sql code to show all the records in this table where order_id's count is more than 1?

Upvotes: 0

Views: 311

Answers (2)

Ankur
Ankur

Reputation: 12774

Try this:

 Select * from table where order_id in (select  distinct order_id
 from table
 group by order_id
 having count(order_id) > 1)

DEMO

Upvotes: 1

Taryn
Taryn

Reputation: 247690

You just need an aggregate function with a GROUP BY

select order_id
from yourtable
group by order_id
having count(order_id) > 1

See SQL Fiddle with Demo

Upvotes: 3

Related Questions