I am trying to execute this query:
SELECT * FROM person JOIN colors ON person.IDA=colors.IDA where Age=20 AND (Color="Black" AND Color="Orange")
So I need a joined table with all the rows that have
- person.Age = 20
- colors.Color = Black
- colors.Color = Orange
I can query correctly only with one condition
SELECT * from person join colors on person.IDA = colors.IDA WHERE Age=20 and colors.Color="Black"
(result)
+-----+------+-----+------+------+-------+
| IDA | Name | Age | ID | IDA | Color |
+-----+------+-----+------+------+-------+
| 1 | John | 20 | 4 | 1 | Black |
| 3 | Bob | 20 | 13 | 3 | Black |
| 3 | Bob | 20 | 16 | 3 | Black |
+-----+------+-----+------+------+-------+
So I'm expecting a result like that when (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 |
+-----+------+-----+------+------+--------+
But I get Impossible WHERE when EXPLAIN the query.
DUMP:
person:
+-------+----------+------+-----+---------+----------------+
| 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)
color
+-------+----------+------+-----+---------+----------------+
| 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)
EDIT:
Thank you for the answers, I think I've picked the wrong query: I'm looking for a result that have BOTH Black and Orange. If I substitute Black with White:
SELECT * FROM person JOIN colors ON person.IDA=colors.IDA where Color="White" OR Color="Orange"
I will get
| 2 | Alex | 21 | 9 | 2 | Orange |
Alex have only the color Orange, not Orange and White
+-----+------+-----+----+-----+--------+
| 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 |
+-----+------+-----+----+-----+--------+