简体   繁体   English

MySQL在同一张表上具有不同WHERE子句的多个SELECT查询

[英]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. 我要建立的MySQL查询需要一些帮助。

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查询,如下所示:

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. 为了使所有这些对于原因有意义,我需要使用第一个SELECT限制三个条件的结果来获取Apple的所有结果。 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. 然后,我需要使用第二个SELECT获取Apple的所有结果,但是这次仅在两个条件下进行限制,并且不包括第一个SELECT的结果,依此类推。

Any idea how to achieve this? 任何想法如何实现这一目标? I tried several different queries, including UNION but somehow cannot get it to display the results I need. 我尝试了几种不同的查询,包括UNION但是以某种方式无法使其显示我所需的结果。

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? 我缺少的东西或者是OR不够?

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 . 而且, UNION应该为您工作了,并且可能更快,因为使用OR时MySQL趋于几乎完全忽略索引。


Edit: You can get the "first" six with an ORDER BY and LIMIT 6 on either the above queries such as this: 编辑:您可以在上面的任一查询中获得带有“ ORDER BY和“ LIMIT 6 ”的“前六个”:

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 .) ...由于单个查询没有顺序,如果第一个条件产生的结果超过六个,则返回的六个结果可能会有所不同,但将与第一个查询/条件不同( 当然,MySQL并不真正保证查询会返回无论如何,在连续执行时,结果将以相同的顺序排列 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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