2016-01-19 6 views
5

Ho una tabella mytable come di seguito;La query MySQL restituisce righe duplicate

╔═════════╦══════╦═════╗ 
║ 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 ║ 
╚═════════╩══════╩═════╝ 

devo l'ingresso 101, 102. Voglio l'output come;

2,5 
6,8 

Ho una query simile;

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

Torna;

2,5 
2,5 
6,8 

Invece di due 2,5, voglio solo una, evitando le righe duplicate. Come posso fare questo?

Ecco il violino http://sqlfiddle.com/#!9/7a78bb/1/0

+5

provare 'selezionare group_concat distinti (ordine lotto per lotto) ' –

+0

TIL che SQLFiddle è una cosa. Eccezionale. – Draco18s

+0

Possibile duplicato di [query MySQL restituisce righe indesiderate sul recupero di righe in base a specifiche combinazioni di tag] (http://stackoverflow.com/questions/34885425/mysql-query-returns-unwanted-rows-on-fetching-rows-based- on-specific-tag-combina) – CodeGodie

risposta

6

Se si desidera distinct poi

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

È necessario utilizzare solo DISTINCT di eliminare i duplicati

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