简体   繁体   中英

UNION vs two queries with big WHERE clauses

I have two queries that are almost identical - one joining the tables gz_life_groups and gz_life_groups2, the other joining gz_life_floaters and gz_life_floaters2. I would like to combine both queries into a single query using UNION, but I haven't been able to jump through all the hoops.

A brief explanation of what the tables do: Imagine navigating to the URL MySite/life/ursus-maritimus. Ursus-maritimus matches a value in the database table field Taxon. So if Ursus maritimus (the polar bear) has been placed in a group titled "White Mammals," it will match values in the table gz_life_groups in the fields Title ("White Mammals") and URL ("white-mammals"). The field Links simply includes links to related pages.

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

This is about as far as my limited knowledge of UNION takes me. I don't know how to add the joins or the WHERE clause...

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

You could try writing like this:

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

If the value of :Phylum, for example, is different for the first and the second part of the UNION , name the 2nd :Phylum to :PylumBelowUnion or something like that and pass an array with values for both :Phylum :PhylumBelowUnion.

I'd also recommend using brackets with your and s and or s like so:

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

I am assuming that's what you want. If not, ignore the advice above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM