UNION vs两个带有大WHERE子句的查询

时间:2015-10-18 02:11:32

标签: php mysql

我有两个几乎相同的查询 - 一个加入表gz_life_groups和gz_life_groups2,另一个加入gz_life_floaters和gz_life_floaters2。我想使用UNION将两个查询合并到一个查询中,但我还没能完成所有的操作。

表格的简要说明:想象一下导航到URL MySite / life / ursus-maritimus。 Ursus-maritimus匹配数据库表字段Taxon中的值。因此,如果Ursus maritimus(北极熊)被放置在一个名为“白色哺乳动物”的小组中,"它将匹配表格gz_life_groups中的值("白色哺乳动物")和URL("白色哺乳动物")。字段链接仅包含指向相关页面的链接。

QUERY 1

$Groups = $pdo->prepare("SELECT G2.URL, G2.Taxon, G1.URL, G1.Title, G1.Links
 FROM gz_life_groups2 AS G2
 LEFT JOIN gz_life_groups G1 ON G1.URL = G2.URL
 WHERE Taxon = :Kingdom AND G2.Live = '1'
 OR Taxon = :Phylum AND G2.Live = '1'
 OR Taxon = :Class AND G2.Live = '1'
 OR Taxon = :Order AND G2.Live = '1'
 OR Taxon = :Family AND G2.Live = '1'
 OR Taxon = :Genus AND G2.Live = '1'
 OR Taxon = :MyURL AND G2.Live = '1'
 GROUP BY G1.URL
 Order By G2.N");
$Groups->execute(array(
 'Kingdom'=>$Kingdom,
 'Phylum'=>$Phylum,
 'Class'=>$Class,
 'Order'=>$Order,
 'Family'=>$Family,
 'Genus'=>$Genus,
 'MyURL'=>$MyURL
));

QUERY 2

$Names = $pdo->prepare("SELECT F2.URL, F2.Taxon, F1.URL, F1.Name, F1.Parent
 FROM gz_life_floaters2 AS F2
 LEFT JOIN gz_life_floaters F1 ON F1.URL = F2.URL
 WHERE Taxon = :Kingdom AND F2.Live = '1'
 OR Taxon = :Phylum AND F2.Live = '1'
 OR Taxon = :Class AND F2.Live = '1'
 OR Taxon = :Order AND F2.Live = '1'
 OR Taxon = :Family AND F2.Live = '1'
 OR Taxon = :Genus AND F2.Live = '1'
 OR Taxon = :MyURL AND F2.Live = '1'
 GROUP BY F1.URL
 Order By F2.N");
$Names->execute(array(
 'Kingdom'=>$Kingdom,
 'Phylum'=>$Phylum,
 'Class'=>$Class,
 'Order'=>$Order,
 'Family'=>$Family,
 'Genus'=>$Genus,
 'MyURL'=>$MyURL
));

这就是我对UNION的有限知识所带来的。我不知道如何添加连接或WHERE子句...

$stm = $pdo->prepare("SELECT *
 FROM (
 SELECT G2.URL, G2.Taxon, G1.URL, G1.Title, NULL AS Parent, G1.Links
 FROM gz_life_groups2 AS G2
 UNION ALL 
 SELECT F2.URL, F2.Taxon, F1.URL, F1.Name AS Title, F1.Parent, NULL AS Links
 FROM gz_life_floaters2 AS F2
) AS Combined
 WHERE Combined.URL LIKE :MyURL");
$stm->execute(array(
'MyURL'=>$MyURL
));

1 个答案:

答案 0 :(得分:1)

你可以尝试这样写:

SELECT G2.URL, G2.Taxon, G1.URL, G1.Title, G1.Links
 FROM gz_life_groups2 AS G2
 LEFT JOIN gz_life_groups G1 ON G1.URL = G2.URL
 WHERE Taxon = :Kingdom AND G2.Live = '1'
 OR Taxon = :Phylum AND G2.Live = '1'
 OR Taxon = :Class AND G2.Live = '1'
 OR Taxon = :Order AND G2.Live = '1'
 OR Taxon = :Family AND G2.Live = '1'
 OR Taxon = :Genus AND G2.Live = '1'
 OR Taxon = :MyURL AND G2.Live = '1'
 GROUP BY G1.URL

UNION

SELECT F2.URL, F2.Taxon, F1.URL, F1.Name, F1.Parent
 FROM gz_life_floaters2 AS F2
 LEFT JOIN gz_life_floaters F1 ON F1.URL = F2.URL
 WHERE Taxon = :Kingdom AND F2.Live = '1'
 OR Taxon = :Phylum AND F2.Live = '1'
 OR Taxon = :Class AND F2.Live = '1'
 OR Taxon = :Order AND F2.Live = '1'
 OR Taxon = :Family AND F2.Live = '1'
 OR Taxon = :Genus AND F2.Live = '1'
 OR Taxon = :MyURL AND F2.Live = '1'
 GROUP BY F1.URL

如果:Phylum的值对于UNION的第一部分和第二部分是不同的,则将第二个:Phylum命名为:PylumBelowUnion或类似的东西,并传递一个数值为两个值的数组:Phylum:PhylumBelowUnion。

我还建议您使用andor这样的括号:

(Taxon = :Kingdom AND F2.Live = '1') OR
(Taxon = :MyURL AND F2.Live = '1') ...

我假设你想要的是什么。如果没有,请忽略上述建议。