SQL查询Impossibile WHERE有两个条件

时间:2017-03-21 19:22:11

标签: mysql join

我正在尝试执行此查询:

SELECT * FROM person JOIN colors ON person.IDA=colors.IDA where Age=20 AND (Color="Black" AND Color="Orange")

所以我需要一个包含

所有行的连接表
  • person.Age = 20
  • colors.Color = Black
  • colors.Color = Orange

我只能用一个条件正确查询

SELECT * from person join colors on person.IDA = colors.IDA WHERE Age=20 and colors.Color="Black"

(结果)

+-----+------+-----+------+------+-------+
| IDA | Name | Age | ID   | IDA  | Color |
+-----+------+-----+------+------+-------+
|   1 | John |  20 |    4 |    1 | Black |
|   3 | Bob  |  20 |   13 |    3 | Black |
|   3 | Bob  |  20 |   16 |    3 | Black |
+-----+------+-----+------+------+-------+

所以我希望得到类似的结果(Color =" Black" AND Color =" Orange"):

+-----+------+-----+------+------+--------+
| IDA | Name | Age | ID   | IDA  | Color  |
+-----+------+-----+------+------+--------+
|   1 | John |  20 |    3 |    1 | Orange |
|   1 | John |  20 |    4 |    1 | Black  |
|   3 | Bob  |  20 |   11 |    3 | Orange |
|   3 | Bob  |  20 |   13 |    3 | Black  |
+-----+------+-----+------+------+--------+

但是在解析查询时我得到了Impossible WHERE。

DUMP:

+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| IDA   | int(50)  | NO   | PRI | NULL    | auto_increment |
| Name  | tinytext | NO   |     | NULL    |                |
| Age   | int(50)  | NO   |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+

+-----+------+-----+
| IDA | Name | Age |
+-----+------+-----+
|   1 | John |  20 |
|   2 | Alex |  21 |
|   3 | Bob  |  20 |
+-----+------+-----+
3 rows in set (0.00 sec)

颜色

+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| ID    | int(50)  | NO   | PRI | NULL    | auto_increment |
| IDA   | int(50)  | NO   |     | NULL    |                |
| Color | tinytext | NO   |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+

+----+-----+--------+
| ID | IDA | Color  |
+----+-----+--------+
|  1 |   1 | White  |
|  2 |   1 | White  |
|  3 |   1 | Orange |
|  4 |   1 | Black  |
|  5 |   2 | Black  |
|  6 |   2 | Black  |
|  7 |   2 | Black  |
|  8 |   2 | Black  |
|  9 |   2 | Orange |
| 10 |   2 | Yellow |
| 11 |   3 | Orange |
| 12 |   3 | White  |
| 13 |   3 | Black  |
| 14 |   3 | Yellow |
| 15 |   1 | Orange |
| 16 |   3 | Black  |
+----+-----+--------+
16 rows in set (0.00 sec)

修改

感谢您的回答,我认为我选择了错误的查询:我正在寻找具有黑色和橙色的结果。如果我用黑色代替黑色:

SELECT * FROM person JOIN colors ON person.IDA=colors.IDA where Color="White" OR  Color="Orange"

我会得到

|   2 | Alex |  21 |  9 |   2 | Orange |

Alex只有橙色,而不是橙色和白色

+-----+------+-----+----+-----+--------+
| IDA | Name | Age | ID | IDA | Color  |
+-----+------+-----+----+-----+--------+
|   1 | John |  20 |  1 |   1 | White  |
|   1 | John |  20 |  2 |   1 | White  |
|   1 | John |  20 |  3 |   1 | Orange |
|   2 | Alex |  21 |  9 |   2 | Orange |
|   3 | Bob  |  20 | 11 |   3 | Orange |
|   3 | Bob  |  20 | 12 |   3 | White  |
|   1 | John |  20 | 15 |   1 | Orange |
+-----+------+-----+----+-----+--------+

6 个答案:

答案 0 :(得分:0)

您需要使用“OR”:

SELECT * FROM person
JOIN colors ON person.IDA=colors.IDA
where person.Age=20 AND (colors.Color="Black" OR colors.Color="Orange")

答案 1 :(得分:0)

最有可能的是,你正在寻找像这样的东西(即基本上是OR条件):

SELECT * from person join colors 
    on person.IDA = colors.IDA 
 WHERE Age=20 and (colors.Color="Black" or colors.Color="Orange")

答案 2 :(得分:0)

SELECT * FROM person JOIN颜色ON person.IDA = colors.IDA,其中Age = 20 AND(Color =“Black”AND Color =“Orange”)。

你需要使用(Color =“Black”OR Color =“Orange”)而不是(Color =“Black”AND Color =“Orange”)

答案 3 :(得分:0)

您需要将AND运算符更改为OR。

WHERE Age=20 OR colors.Color="Black"

答案 4 :(得分:0)

您是否正在寻找黑色和橙色的结果。如果是这样,请使用OR运算符而不是AND。

答案 5 :(得分:0)

WHERE是不可能的,因为您要求Color同时为黑色和橙色。

您可以使用OR代替AND来搜索任一颜色。

SELECT * FROM person JOIN colors
  ON person.IDA=colors.IDA
  WHERE
    Age=20 AND
    (Color="Black" OR Color="Orange")