我需要修复一个查询,不知道怎么做。实际上查询是这个
(SELECT qrliststgo.ordenser,qrliststgo.razon,qrliststgo.emision,qrliststgo.despacho,hitos.fecha FROM `qrliststgo`,`hitos` WHERE `qrliststgo`.`tipotransporte`='I' AND `hitos`.`idplace`=`qrliststgo`.`sigla` AND `hitos`.`guia`=`qrliststgo`.`guia`) UNION (SELECT qrlistvalpo.ordenser,qrlistvalpo.razon,qrlistvalpo.emision,qrlistvalpo.despacho,hitos.fecha FROM `qrlistvalpo`,`hitos` WHERE `tipotransporte`='I' AND `hitos`.`idplace`=`qrlistvalpo`.`sigla` AND `hitos`.`guia`=`qrlistvalpo`.`guia`) UNION (SELECT qrlistsananto.ordenser,qrlistsananto.razon,qrlistsananto.emision,qrlistsananto.despacho,hitos.fecha FROM `qrlistsananto`,`hitos` WHERE `qrlistsananto`.`tipotransporte`='I' AND `hitos`.`idplace`=`qrlistsananto`.`sigla` AND `hitos`.`guia`=`qrlistsananto`.`guia`) UNION (SELECT qrlistlocalstgo.ordenser,qrlistlocalstgo.razon,qrlistlocalstgo.emision,qrlistlocalstgo.despacho,hitos.fecha FROM `qrlistlocalstgo`,`hitos` WHERE `qrlistlocalstgo`.`tipotransporte`='I' AND `hitos`.`idplace`=`qrlistlocalstgo`.`sigla` AND `hitos`.`guia`=`qrlistlocalstgo`.`guia`) UNION (SELECT qrlistlocalvalpo.ordenser,qrlistlocalvalpo.razon,qrlistlocalvalpo.emision,qrlistlocalvalpo.despacho,hitos.fecha FROM `qrlistlocalvalpo`,`hitos` WHERE `qrlistlocalvalpo`.`tipotransporte`='I' AND `hitos`.`idplace`=`qrlistlocalvalpo`.`sigla` AND `hitos`.`guia`=`qrlistlocalvalpo`.`guia`) UNION (SELECT qrlistlocalsananto.ordenser,qrlistlocalsananto.razon,qrlistlocalsananto.emision,qrlistlocalsananto.despacho,hitos.fecha FROM `qrlistlocalsananto`,`hitos` WHERE `qrlistlocalsananto`.`tipotransporte`='I' AND `hitos`.`idplace`=`qrlistlocalsananto`.`sigla` AND `hitos`.`guia`=`qrlistlocalsananto`.`guia`)
是的,我知道它很长,基本上,我需要找到所有的行" qrliststgo"它匹配2列的" hitos"。
(SELECT qrliststgo.ordenser,qrliststgo.razon,qrliststgo.emision,qrliststgo.despacho,hitos.fecha FROM `qrliststgo`,`hitos` WHERE `qrliststgo`.`tipotransporte`='I' AND `hitos`.`idplace`=`qrliststgo`.`sigla` AND `hitos`.`guia`=`qrliststgo`.`guia`)
然后使用UNION对名为" qrlistvalpo"的另一个表执行相同的操作。和" hitos"试。
我遇到的问题是,在" hitos"中,有3行匹配,来自" qrliststgo"的一行,所以它是"重复"。 .. 3行具有相同的信息,除了最后一个更改,因为它找到了3个结果" hitos"。
我该怎么做?
(我需要这个用于php的excell导出)
更新
我需要的是显示qrlist中的所有行,其中hitos中至少有一行qrlist.sigla = hitos.idplace和qrlist.guia = hitos.guia。
如果匹配的hitos中有多个结果,则显示第一个结果并跳过其他结果,然后检查qrlist中的另一个结果。
不知道我是否正确解释
答案 0 :(得分:0)
查看以下内容是否包含您之后的内容:
(SELECT ordenser, razon, emision, despacho, fecha FROM qrliststgo
WHERE (sigla, guia) IN (SELECT DISTINCT idplace, guia FROM hitos))
UNION (SELECT ordenser, razon, emision, despacho, fecha FROM qrlistvalpo
WHERE (sigla, guia) IN (SELECT DISTINCT idplace, guia FROM hitos))
UNION (SELECT ordenser, razon, emision, despacho, fecha FROM qrlistsananto
WHERE (sigla, guia) IN (SELECT DISTINCT idplace, guia FROM hitos))
UNION (SELECT ordenser, razon, emision, despacho, fecha FROM qrlistlocalstgo
WHERE (sigla, guia) IN (SELECT DISTINCT idplace, guia FROM hitos))
UNION (SELECT ordenser, razon, emision, despacho, fecha FROM qrlistlocalvalpo
WHERE (sigla, guia) IN (SELECT DISTINCT idplace, guia FROM hitos))
UNION (SELECT ordenser, razon, emision, despacho, fecha FROM qrlistlocalsananto
WHERE (sigla, guia) IN (SELECT DISTINCT idplace, guia FROM hitos));