我正在运行一个选择订单详情的查询,我想只查看经过多个阶段的订单。我的数据如下:
id | order_id | action
1 100 1
2 100 2
3 100 4
4 101 1
5 102 2
6 103 1
7 103 2
这样只会选择order_id 100和103的行。这需要嵌套在更大的查询中。
答案 0 :(得分:2)
您可以使用子查询来获取具有多个阶段的订单:
SELECT order_id
FROM your_table
GROUP BY order_id
HAVING COUNT(*)>1
然后你可以将这个结果加入你的桌子:
SELECT o.*
FROM yourtable AS o INNER JOIN (
SELECT order_id
FROM your_table
GROUP BY order_id
HAVING COUNT(*)>1
) dup ON o.order_id = dup.order_id
答案 1 :(得分:0)
将group by
与count
和having
select *,count(order_id) as total from table
group by order_id
having total > 1
答案 2 :(得分:0)
您可以尝试此查询:
select * from your_table
where ( select count(*) from your_table internal_table
where your_table .order_id = internal_table.order_id
) > 1