MySQL查询返回重复行

5

我有一张名为mytable的表,结构如下:

╔═════════╦══════╦═════╗
║ product ║ tag  ║ lot ║
╠═════════╬══════╬═════╣
║ 11111012   ║ 
║ 11111025   ║ 
║ 22221036   ║ 
║ 33331042   ║  
║ 44441012   ║ 
║ 55551012   ║ 
║ 55551025   ║ 
║ 66661022   ║ 
║ 66661035   ║
║ 77771012   ║ 
║ 77771025   ║ 
║ 77771036   ║ 
║ 88881011   ║ 
║ 88881023   ║ 
║ 88881035   ║ 
║ 99991016   ║ 
║ 99991028   ║
╚═════════╩══════╩═════╝

我有输入 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.5,而不是两个,避免出现重复行。我该怎么做?

这是fiddle链接:http://sqlfiddle.com/#!9/7a78bb/1/0


5
尝试使用 select distinct group_concat(lot order by lot)。该语句的作用是将 "lot" 字段中的所有不同值拼接成一个字符串,并按 "lot" 字段进行排序。 - Giorgos Betsos
今天我了解到SQLFiddle是一个很棒的工具。 - Draco18s no longer trusts SE
@GiorgosBetsos,它起作用了。请将其发布为答案,以便我可以接受。 - Alfred
2个回答

7
如果您想要“distinct”,那么...
select distinct group_concat(lot order by lot)
from `mytable`
group by product
having group_concat(tag order by tag) = '101,102';

0

你只需要使用DISTINCT来消除重复项

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接