简体   繁体   中英

MySQL Multiple SELECT queries with different WHERE clause on the same table

I need some help on a MySQL query that I am trying to build.

The is the table I have:

+----+-----------+---------+-----------+---------+------------+-----------+
| id |  fruit    | color   |  shape    | country |    vit     |   city    |
+----+-----------+---------+-----------+---------+------------+-----------+
| id |  Apple    | red     | spherical | UK      |     A      | London    |
| id |  Orange   | orange  | circular  | USA     |     C      | New York  |
| id |  Apple    | green   | multiple  | Mexico  |     C      | Cancun    |
| id |  Apple    | red     | spherical | Canada  |     B      | Ottawa    |
+----+-----------+---------+-----------+---------+------------+-----------+

Now I want to use one query to perform multiple SELECT queries like so:

SELECT * from tbl_fruits WHERE fruit = "Apple" AND vit = "A" AND city = "London";
SELECT * from tbl_fruits WHERE fruit = "Apple" AND vit = "B" AND city != "London;
SELECT * from tbl_fruits WHERE fruit = "Apple" AND vit != "A" AND vit != "B" AND city != "London";

The resulting table would be:

+----+-----------+---------+-----------+---------+------------+-----------+
| id |  fruit    | color   |  shape    | country |    vit     |   city    |
+----+-----------+---------+-----------+---------+------------+-----------+
| id |  Apple    | red     | spherical | UK      |     A      | London    |
| id |  Apple    | green   | multiple  | Mexico  |     C      | Cancun    |
| id |  Apple    | red     | spherical | Canada  |     B      | Ottawa    |
+----+-----------+---------+-----------+---------+------------+-----------+

In order for all of this to make sense as to why, I need to get all results for Apple with the first SELECT limiting on three conditions. Then I need to get all results for Apple with the second SELECT but this time limiting only on two conditions AND excluding the results from the first SELECT and so on.

Any idea how to achieve this? I tried several different queries, including UNION but somehow cannot get it to display the results I need.

Use select union and not in

SELECT * from tbl_fruits WHERE fruit = "Apple" AND vit = "A" AND city = "London"
union
SELECT * from tbl_fruits WHERE fruit = "Apple" AND vit = "B" AND city != "London
union
SELECT * from tbl_fruits WHERE fruit = "Apple" AND vit NOT IN ( "A" , "B" ) AND city != "London";

Am I missing something or is OR not enough?

SELECT * 
FROM tbl_fruits 
WHERE (fruit = "Apple" AND vit = "A" AND city = "London")
   OR (fruit = "Apple" AND vit = "B" AND city != "London)
   OR (fruit = "Apple" AND vit != "A" AND vit != "B" AND city != "London")
;

which could be refactored to...

SELECT * 
FROM tbl_fruits 
WHERE fruit = "Apple" 
   AND (    (vit = "A" AND city = "London")
         OR (vit = "B" AND city != "London)
         OR (vit != "A" AND vit != "B" AND city != "London")
       )
;

...pretty sure the criteria on all those vit,city combinations can be condensed as well.

Also, UNION should have worked for you, and might be faster since MySQL tends to almost completely ignore indexes when using OR .


Edit: You can get the "first" six with an ORDER BY and LIMIT 6 on either the above queries such as this:

ORDER BY (fruit = "Apple" AND vit = "A" AND city = "London") DESC
   , (fruit = "Apple" AND vit = "B" AND city != "London) DESC
   , (fruit = "Apple" AND vit != "A" AND vit != "B" AND city != "London") DESC
LIMIT 6

...since the individual queries had no order, if the first condition yields more than six results, the six returned may differ but will be from the first query/condition ( of course, MySQL doesn't actually guarantee the a query will return the results in the same order on consecutive executions anyway .)

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