*!* 建立测试数据
CREATE CURSOR 存货(入庫号 C(3),形号 C(3),材质 C(2),颜色 C(2))
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('100','136','CO','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('101','136','TU','RE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('102','138','CO','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('103','138','MU','RE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('104','221','CO','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('105','221','MU','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('106','135','CO','BU')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('107','135','CO','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('108','222','TU','RE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('109','222','TU','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('110','222','MU','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('111','223','TU','RE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('112','221','MU','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('113','221','CO','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('114','136','TU','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('115','136','CO','BU')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('116','138','MU','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('117','135','TU','RE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('118','223','CO','BU')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('119','223','CO','BK')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('120','222','MU','RE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('121','330','TU','RE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('122','330','MU','RE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('123','331','TU','YE')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('124','335','FE','BU')
INSERT INTO 存货(入庫号,形号,材质,颜色) VALUES ('125','335','TU','BU')
*!* 查询
SELECT t0.* FROM (SELECT t1.* FROM 存货 as t1 ;
INNER JOIN (SELECT 形号,材质 FROM 存货 GROUP BY 形号,材质 HAVING COUNT(形号) >1) as t2 ;
ON t1.形号==t2.形号 AND t1.材质==t2.材质 ;
UNION ;
SELECT t3.* FROM 存货 as t3 ;
INNER JOIN (SELECT 形号,颜色 FROM 存货 GROUP BY 形号,颜色 HAVING COUNT(形号) >1) as t4 ;
ON t3.形号==t4.形号 AND t3.颜色==t4.颜色 ) as t0 ;
ORDER BY t0.形号,t0.材质,t0.颜色