MySQL query returns duplicate rows

I have a mytable table as shown below;

 ╔═════════╦══════╦═════╗ β•‘ 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 β•‘ β•šβ•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•©β•β•β•β•β•β• 

I have input 101 , 102 . I want the result to be as follows:

 2,5 6,8 

I have a query like:

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

It returns:

 2,5 2,5 6,8 

Instead of two 2,5 , I want only one, avoiding duplicate lines. How can i do this?

Here is the fiddle http://sqlfiddle.com/#!9/7a78bb/1/0

+5
source share
2 answers

If you want distinct , then

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

You just need to use DISTINCT to eliminate duplicates

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

Source: https://habr.com/ru/post/1240959/


All Articles