gpt4 book ai didi

mysql - 自连接返回比预期更多的行

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:03 24 4
gpt4 key购买 nike

我是自连接的新手,并制作了以下示例表:

+-----------+-------------+
| name | location |
+-----------+-------------+
| Robert | Guadalajara |
| Manuel | Guadalajara |
| Dalia | Guadalajara |
| Alejandra | Guadalajara |
| Luis | Guadalajara |
| Monica | Guadalajara |
| Claudia | Guadalajara |
| Scartlet | Guadalajara |
| Sergio | Guadalajara |
| Rick | Mexico City |
| Rene | Mexico City |
| Ramon | Culiacan |
| Junior | Culiacan |
| Kasandra | Culiacan |
| Emma | Culiacan |
| Johnatha | Dunedin |
| Miriam | Largo |
| Julie | Largo |
+-----------+-------------+

18 行

我想运行一个简单的查询,将具有相同位置的人与以下查询相匹配:

SELECT users1.name, users1.location, users2.name, users2.location
FROM users users1, users users2
WHERE users1.location = users2.location;

我的预期:

+-----------+-------------++-----------+-------------+                  
| name | location || name | location |
+-----------+-------------++-----------+-------------+
| Robert | Guadalajara || Robert | Guadalajara |
| Manuel | Guadalajara || Manuel | Guadalajara |
| Dalia | Guadalajara || Dalia | Guadalajara |
| Alejandra | Guadalajara || Alejandra | Guadalajara |
| Luis | Guadalajara || Luis | Guadalajara |
| Monica | Guadalajara || Monica | Guadalajara |
| Claudia | Guadalajara || Claudia | Guadalajara |
| Scartlet | Guadalajara || Scartlet | Guadalajara |
| Sergio | Guadalajara || Sergio | Guadalajara |
| Rick | Mexico City || Rick | Mexico City |
| Rene | Mexico City || Rene | Mexico City |
| Ramon | Culiacan || Ramon | Culiacan |
| Junior | Culiacan || Junior | Culiacan |
| Kasandra | Culiacan || Kasandra | Culiacan |
| Emma | Culiacan || Emma | Culiacan |
| Johnatha | Dunedin || Johnatha | Dunedin |
| Miriam | Largo || Miriam | Largo |
| Julie | Largo || Julie | Largo |
+-----------+-------------++-----------+-------------+

我得到的:

+-----------+-------------+-----------+-------------+
| name | location | name | location |
+-----------+-------------+-----------+-------------+
| Robert | Guadalajara | Robert | Guadalajara |
| Manuel | Guadalajara | Robert | Guadalajara |
| Dalia | Guadalajara | Robert | Guadalajara |
| Alejandra | Guadalajara | Robert | Guadalajara |
| Luis | Guadalajara | Robert | Guadalajara |
| Monica | Guadalajara | Robert | Guadalajara |
| Claudia | Guadalajara | Robert | Guadalajara |
| Scartlet | Guadalajara | Robert | Guadalajara |
| Sergio | Guadalajara | Robert | Guadalajara |
| Robert | Guadalajara | Manuel | Guadalajara |
| Manuel | Guadalajara | Manuel | Guadalajara |
| Dalia | Guadalajara | Manuel | Guadalajara |
| Alejandra | Guadalajara | Manuel | Guadalajara |
| Luis | Guadalajara | Manuel | Guadalajara |
| Monica | Guadalajara | Manuel | Guadalajara |
| Claudia | Guadalajara | Manuel | Guadalajara |
| Scartlet | Guadalajara | Manuel | Guadalajara |
| Sergio | Guadalajara | Manuel | Guadalajara |
| Robert | Guadalajara | Dalia | Guadalajara |
| Manuel | Guadalajara | Dalia | Guadalajara |
| Dalia | Guadalajara | Dalia | Guadalajara |
| Alejandra | Guadalajara | Dalia | Guadalajara |
| Luis | Guadalajara | Dalia | Guadalajara |
| Monica | Guadalajara | Dalia | Guadalajara |
| Claudia | Guadalajara | Dalia | Guadalajara |
| Scartlet | Guadalajara | Dalia | Guadalajara |
| Sergio | Guadalajara | Dalia | Guadalajara |
| Robert | Guadalajara | Alejandra | Guadalajara |
| Manuel | Guadalajara | Alejandra | Guadalajara |
| Dalia | Guadalajara | Alejandra | Guadalajara |
| Alejandra | Guadalajara | Alejandra | Guadalajara |
| Luis | Guadalajara | Alejandra | Guadalajara |
| Monica | Guadalajara | Alejandra | Guadalajara |
| Claudia | Guadalajara | Alejandra | Guadalajara |
| Scartlet | Guadalajara | Alejandra | Guadalajara |
| Sergio | Guadalajara | Alejandra | Guadalajara |
| Robert | Guadalajara | Luis | Guadalajara |
| Manuel | Guadalajara | Luis | Guadalajara |
| Dalia | Guadalajara | Luis | Guadalajara |
| Alejandra | Guadalajara | Luis | Guadalajara |
| Luis | Guadalajara | Luis | Guadalajara |
| Monica | Guadalajara | Luis | Guadalajara |
| Claudia | Guadalajara | Luis | Guadalajara |
| Scartlet | Guadalajara | Luis | Guadalajara |
| Sergio | Guadalajara | Luis | Guadalajara |
| Robert | Guadalajara | Monica | Guadalajara |
| Manuel | Guadalajara | Monica | Guadalajara |
| Dalia | Guadalajara | Monica | Guadalajara |
| Alejandra | Guadalajara | Monica | Guadalajara |
| Luis | Guadalajara | Monica | Guadalajara |
| Monica | Guadalajara | Monica | Guadalajara |
| Claudia | Guadalajara | Monica | Guadalajara |
| Scartlet | Guadalajara | Monica | Guadalajara |
| Sergio | Guadalajara | Monica | Guadalajara |
| Robert | Guadalajara | Claudia | Guadalajara |
| Manuel | Guadalajara | Claudia | Guadalajara |
| Dalia | Guadalajara | Claudia | Guadalajara |
| Alejandra | Guadalajara | Claudia | Guadalajara |
| Luis | Guadalajara | Claudia | Guadalajara |
| Monica | Guadalajara | Claudia | Guadalajara |
| Claudia | Guadalajara | Claudia | Guadalajara |
| Scartlet | Guadalajara | Claudia | Guadalajara |
| Sergio | Guadalajara | Claudia | Guadalajara |
| Robert | Guadalajara | Scartlet | Guadalajara |
| Manuel | Guadalajara | Scartlet | Guadalajara |
| Dalia | Guadalajara | Scartlet | Guadalajara |
| Alejandra | Guadalajara | Scartlet | Guadalajara |
| Luis | Guadalajara | Scartlet | Guadalajara |
| Monica | Guadalajara | Scartlet | Guadalajara |
| Claudia | Guadalajara | Scartlet | Guadalajara |
| Scartlet | Guadalajara | Scartlet | Guadalajara |
| Sergio | Guadalajara | Scartlet | Guadalajara |
| Robert | Guadalajara | Sergio | Guadalajara |
| Manuel | Guadalajara | Sergio | Guadalajara |
| Dalia | Guadalajara | Sergio | Guadalajara |
| Alejandra | Guadalajara | Sergio | Guadalajara |
| Luis | Guadalajara | Sergio | Guadalajara |
| Monica | Guadalajara | Sergio | Guadalajara |
| Claudia | Guadalajara | Sergio | Guadalajara |
| Scartlet | Guadalajara | Sergio | Guadalajara |
| Sergio | Guadalajara | Sergio | Guadalajara |
| Rick | Mexico City | Rick | Mexico City |
| Rene | Mexico City | Rick | Mexico City |
| Rick | Mexico City | Rene | Mexico City |
| Rene | Mexico City | Rene | Mexico City |
| Ramon | Culiacan | Ramon | Culiacan |
| Junior | Culiacan | Ramon | Culiacan |
| Kasandra | Culiacan | Ramon | Culiacan |
| Emma | Culiacan | Ramon | Culiacan |
| Ramon | Culiacan | Junior | Culiacan |
| Junior | Culiacan | Junior | Culiacan |
| Kasandra | Culiacan | Junior | Culiacan |
| Emma | Culiacan | Junior | Culiacan |
| Ramon | Culiacan | Kasandra | Culiacan |
| Junior | Culiacan | Kasandra | Culiacan |
| Kasandra | Culiacan | Kasandra | Culiacan |
| Emma | Culiacan | Kasandra | Culiacan |
| Ramon | Culiacan | Emma | Culiacan |
| Junior | Culiacan | Emma | Culiacan |
| Kasandra | Culiacan | Emma | Culiacan |
| Emma | Culiacan | Emma | Culiacan |
| Johnatha | Dunedin | Johnatha | Dunedin |
| Miriam | Largo | Miriam | Largo |
| Julie | Largo | Miriam | Largo |
| Miriam | Largo | Julie | Largo |
| Julie | Largo | Julie | Largo |
+-----------+-------------+-----------+-------------+

除了建议的解决方案之外,有人可以向我解释为什么我会得到这个结果吗?我想知道这是如何发生的以及为什么会发生这种情况。

谢谢。

最佳答案

您在位置上匹配,这发生了多次。这与您当前的结果相乘。

您的预期结果是名称连接:

SELECT users1.name, users1.location, users2.name, users2.location
FROM users users1, users users2
WHERE users1.name = users2.name;

查询的工作原理:

在数据集中,WHERE 中的列是匹配的。这相当于一个 JOIN:

SELECT users1.name, users1.location, users2.name, users2.location
FROM users users1
INNER JOIN users users2 ON users1.name = users2.name;

这是现在比较常用的语法。这两个查询是相同的,但是使用 JOIN 它变得更具可读性和灵 active 。

JOIN 中的匹配是在 users1.name 等于 users2.name 时进行的。然后显示找到的结果:

| Robert | Guadalajara |

例如。

如果使用位置而不是名称运行查询:

INNER users users2 ON users1.location = users2.location;

SQL 匹配位置。因此,对于一个更短、更易读的例子,想象一下这个短表。添加列 ID 以显示收集结果的方式:

+-----------+-------------+----+
| name | location | ID |
+-----------+-------------+----+
| Robert | Guadalajara | 1 |
| Manuel | Guadalajara | 2 |

这是在位置上匹配的,它出现了两次:

Id 1 is matched with Id 1 and Id 2 (using location): Resulting in 2 records for location:

 | Robert    | Guadalajara | 1  |
| Manuel | Guadalajara | 1 |

Id 2 is matched with Id 1 and Id 2 (using location): Resulting in 2 records for location:

 | Robert    | Guadalajara | 2  |
| Manuel | Guadalajara | 2 |

那么最后的结果就是:

| Robert    | Guadalajara | 1  |
| Manuel | Guadalajara | 1 |
| Robert | Guadalajara | 2 |
| Manuel | Guadalajara | 2 |

因此您的原始记录成倍增加。

关于mysql - 自连接返回比预期更多的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31639221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com