已阅读 Meta 上的 your question 关于这个特定问题的内容,让我解释一下为什么所有三个答案都确实正确 - 就像您解决问题的方式一样。
我已经包含了所有三个答案的示例以及他们正在处理的架构:
Database changed
mysql> create table carpet(id int(3), material varchar(10), color varchar(15));
Query OK, 0 rows affected (0.02 sec)
mysql> create table curtain(id int(3), material varchar(10), color varchar(15));
Query OK, 0 rows affected (0.00 sec)
(一组插入语句)
mysql> select * from carpet;
+------+-----------+--------------+
| id | material | color |
+------+-----------+--------------+
| 1 | wool | Light Yellow |
| 2 | wool | Beige |
| 3 | polyester | Light Yellow |
| 4 | polyester | Light Red |
+------+-----------+--------------+
4 rows in set (0.00 sec)
mysql> select * from curtain;
+------+----------+--------------+
| id | material | color |
+------+----------+--------------+
| 1 | Velvet | Purple |
| 2 | cotton | White |
| 3 | cotton | Light Yellow |
| 4 | cotton | Light Blue |
+------+----------+--------------+
4 rows in set (0.00 sec)
一个 intersect 使用两个 select 语句并返回匹配结果。在这种情况下,您要查找所有匹配颜色为“浅黄色”的行。
我不能在 MySQL 中给你一个例子,因为它不支持它(正如你在下面看到的,它不需要给出相同的结果)。
两个 select 语句的联合查询,每个语句都有一个 where 子句,只允许“浅黄色”的颜色将返回相同的数据。虽然联合可用于返回不匹配的数据,但每个 select 语句中的 where 子句意味着它只会返回您想要的行。
mysql> select id, material, color from carpet
-> union
-> select id, material, color from curtain;
+------+-----------+--------------+
| id | material | color |
+------+-----------+--------------+
| 1 | wool | Light Yellow |
| 2 | wool | Beige |
| 3 | polyester | Light Yellow |
| 4 | polyester | Light Red |
| 1 | Velvet | Purple |
| 2 | cotton | White |
| 3 | cotton | Light Yellow |
| 4 | cotton | Light Blue |
+------+-----------+--------------+
8 rows in set (0.00 sec)
哇,这很糟糕,对吧?当然,我们没有指定 where 子句:
mysql> select id, material, color from carpet where color='Light Yellow'
-> union
-> select id, material, color from curtain where color='Light Yellow';
+------+-----------+--------------+
| id | material | color |
+------+-----------+--------------+
| 1 | wool | Light Yellow |
| 3 | polyester | Light Yellow |
| 3 | cotton | Light Yellow |
+------+-----------+--------------+
3 rows in set (0.00 sec)
两个表之间的颜色连接将允许您在单行数据中返回两个表中的行。您可以为项目颜色指定两个表的连接,并使用 where 子句仅返回您正在查找的行。
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a join carpet b on a.color=b.color;
+------+----------+--------------+------+-----------+
| id | material | color | id | material |
+------+----------+--------------+------+-----------+
| 3 | cotton | Light Yellow | 1 | wool |
| 3 | cotton | Light Yellow | 3 | polyester |
+------+----------+--------------+------+-----------+
2 rows in set (0.00 sec)
如您所见,这仅返回具有匹配颜色的行,并允许您在结果集中的单行中包含两个表中的列。
现在,我显然没有很好地计划这个,因为除了两个表中的“浅黄色”之外,我没有其他匹配的结果,所以如果我在其中添加更多条目,我们会得到:
mysql> select * from curtain;
+------+----------+--------------+
| id | material | color |
+------+----------+--------------+
| 1 | Velvet | Purple |
| 2 | cotton | White |
| 3 | cotton | Light Yellow |
| 4 | cotton | Light Blue |
| 5 | Wool | White |
| 6 | Fluff | Beige |
+------+----------+--------------+
6 rows in set (0.00 sec)
mysql> select * from carpet;
+------+-----------+--------------+
| id | material | color |
+------+-----------+--------------+
| 1 | wool | Light Yellow |
| 2 | wool | Beige |
| 3 | polyester | Light Yellow |
| 4 | polyester | Light Red |
| 5 | Fluff | Light Blue |
+------+-----------+--------------+
5 rows in set (0.00 sec)
现在我们可以再次运行它,这次得到:
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a join carpet b on a.color=b.color;
+------+----------+--------------+------+-----------+
| id | material | color | id | material |
+------+----------+--------------+------+-----------+
| 3 | cotton | Light Yellow | 1 | wool |
| 3 | cotton | Light Yellow | 3 | polyester |
| 4 | cotton | Light Blue | 5 | Fluff |
| 6 | Fluff | Beige | 2 | wool |
+------+----------+--------------+------+-----------+
4 rows in set (0.00 sec)
哦不!
现在我们将 join 和 where 子句一起使用:
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a join carpet b on a.color=b.color
-> where a.color='Light Yellow';
+------+----------+--------------+------+-----------+
| id | material | color | id | material |
+------+----------+--------------+------+-----------+
| 3 | cotton | Light Yellow | 1 | wool |
| 3 | cotton | Light Yellow | 3 | polyester |
+------+----------+--------------+------+-----------+
2 rows in set (0.00 sec)
您会发现,在 SQL 中,通过不同方式获得相同结果的方法通常比表中相同数据的变体要多。
编辑:好的,所以如果您只想要 all 数据匹配的行,只需将其包含在连接语法中:
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a
-> join carpet b on a.color=b.color
-> and a.id=b.id
-> where a.color='Light Yellow';
+------+----------+--------------+------+-----------+
| id | material | color | id | material |
+------+----------+--------------+------+-----------+
| 3 | cotton | Light Yellow | 3 | polyester |
+------+----------+--------------+------+-----------+
1 row in set (0.00 sec)
如您所见,现在我们告诉联接,id 和 color 字段必须在两个表之间匹配 - 结果不言自明。现在,在这种情况下,我在技术上 仍然 没有匹配所有列,因为材料不同。如果您想进一步匹配,查询将不会返回任何结果,因为我没有匹配 id、material 和 color 匹配的记录,但语法如下:
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a
-> join carpet b on a.color=b.color
-> and a.id=b.id
-> and a.material=b.material
-> where a.color='Light Yellow';
Empty set (0.00 sec)
尽管如此,但在大多数情况下,您不希望 所有 列都匹配。很多时候,表都有一个仅用于该表的 ID,并且是一个自动递增的值。您想使用它来识别 that 表中的唯一行,但不使用它来匹配不相关的表。如果有的话,我会建议您在材料和颜色上进行匹配 - 但不要将 ID 放在其中。