2015-12-09 35 views
1

我正在運行一個查詢來選擇訂單的詳細信息,並且我只想看到已經經歷了多個階段的訂單。我的數據如下:MySQL - 從查詢中刪除非重複的行

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的行纔會被選中。這需要嵌套在更大的查詢中。

回答

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 
0

使用group bycounthaving

select *,count(order_id) as total from table 
group by order_id 
having total > 1 
0

你可以試試這個查詢:

select * from your_table 
where (select count(*) from your_table internal_table 
where your_table .order_id = internal_table.order_id 

) > 1