我需要修复查询,但不知道如何。实际上查询是这样的

(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列和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”中有1行,因此被“复制”了……3行具有相同的信息,但最后一条因为发现而改变了3个结果为“ hitos”。

我该怎么做?

(我需要这个来使PHP取消导出)

更新:

我需要的是显示来自qrlist的所有行,其中在hitos中至少有一行qrlist.sigla = hitos.idplace和qrlist.guia = hitos.guia。

如果Hitos中有多个匹配的结果,请显示第一个并跳过其他结果,然后从qrlist检查另一个结果。

不知道我是否解释正确

最佳答案

查看以下内容是否满足您的要求:

      (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));

关于php - MySQL查询多表与联合和限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31384399/

10-16 18:20