MySQL查询1个表内1-2个字段内容相同且1个字段内容不同的数据,并全部输出

作者: 坎肩儿 分类: SQL语句 发布时间: 2024-01-28 19:18

在开发公司的一个BOM管理的系统时遇到了这个情况:MySQL查询1个表内1-2个字段内容相同且1个字段内容不同的数据,并全部输出

直接上代码:

网上的代码,不是很完善。

select * from bom_mingxi a,bom_mingxi b where a.mingcheng = b.mingcheng and a.bianhao <> b.bianhao

select * from bom_mingxi a,bom_mingxi b where a.mingcheng = b.mingcheng and a.guige = b.guige and a.bianhao <> b.bianhao

输出2个字段相同,且1个字段不同的的数据(有效):
select * from bom_mingxi m where exists ( select 1 from bom_mingxi m2 where m.mingcheng=m2.mingcheng and m.guige=m2.guige and m.bianhao<>m2.bianhao) order by mingcheng;

亲测有效。