2016-01-19 9 views
5

私はテーブルを持っていますmytable以下のように;MySQLクエリが重複行を返す

╔═════════╦══════╦═════╗ 
║ product ║ tag ║ lot ║ 
╠═════════╬══════╬═════╣ 
║ 1111 ║ 101 ║ 2 ║ 
║ 1111 ║ 102 ║ 5 ║ 
║ 2222 ║ 103 ║ 6 ║ 
║ 3333 ║ 104 ║ 2 ║ 
║ 4444 ║ 101 ║ 2 ║ 
║ 5555 ║ 101 ║ 2 ║ 
║ 5555 ║ 102 ║ 5 ║ 
║ 6666 ║ 102 ║ 2 ║ 
║ 6666 ║ 103 ║ 5 ║ 
║ 7777 ║ 101 ║ 2 ║ 
║ 7777 ║ 102 ║ 5 ║ 
║ 7777 ║ 103 ║ 6 ║ 
║ 8888 ║ 101 ║ 1 ║ 
║ 8888 ║ 102 ║ 3 ║ 
║ 8888 ║ 103 ║ 5 ║ 
║ 9999 ║ 101 ║ 6 ║ 
║ 9999 ║ 102 ║ 8 ║ 
╚═════════╩══════╩═════╝ 

私は入力101102を持っています。私はそのような出力をしたい。

2,5 
6,8 

私のようなクエリがあります。

select group_concat(lot order by lot) 
from `mytable` 
group by product 
having group_concat(tag order by tag) = '101,102'; 

返されます。

2,5 
2,5 
6,8 

の代わりに2 2,5、私は重複行を避け、一つだけをしたいです。これどうやってするの?ここで

フィドルhttp://sqlfiddle.com/#!9/7a78bb/1/0

+5

を排除するためにDISTINCTを使用する必要があります) ' –

+0

SQLFiddleは事実です。驚くばかり。 – Draco18s

+0

[MySQLクエリは、特定のタグの組み合わせに基づいて行をフェッチする際に不要な行を返す可能性があります](http://stackoverflow.com/questions/34885425/mysql-query-returns-unwanted-rows-on-fetching-rows-based- on-specific-tag-combina) – CodeGodie

答えて

6

あなたはdistinctその後、

select distinct group_concat(lot order by lot) 
from `mytable` 
group by product 
having group_concat(tag order by tag) = '101,102'; 
0

をしたい場合はあなただけの多くで明確なGROUP_CONCAT(ロットオーダーを選択し `てみてください重複

select DISTINCT group_concat(lot order by lot) 
from `mytable` 
group by product 
having group_concat(tag order by tag) = '101,102'; 
関連する問題