mysql - 为什么 MySql LEFT OUTER JOIN 返回 20 倍以上的行?

标签 mysql sql left-join

好的,我在以下 LEFT OUTER JOIN 查询中返回了 400,816 行

SELECT 
    `inventory`.`inventory_id`,
    `inventory`.`inventory_units_in_stock`,
    `inventory`.`sire_name`,
    `inventory`.`owner_id`,
    `inventory`.`facility`,
    `inventory`.`breed`,
    `inventory`.`cane_number`,
    `inventory`.`collection_date`,
    `inventory`.`inventory_temporary_location`,
    `inventory`.`inventory_tank`,
    `inventory`.`inventory_bay`,
    `inventory`.`inventory_canister`,
    `inventory`.`inventory_remarks`,
    `inventory`.`inventory_update`,
    `inventory`.`inventory_create`,
    `inventory`.`inventory_user_update`,
    `inventory`.`inventory_user_create`,
    `collections`.`collectionId`
FROM `inventory`
LEFT JOIN
    `collections` ON Date(`collections`.`collection_date`) = Date(`inventory`.`collection_date`)

inventory 表有 20,867 条记录,collections 表有 15,326 条记录。那么上面的查询是如何返回 400,816 条记录的呢?

inventory 和 collections 表中的 collection_date 是 MySql 数据类型 = DATE。我在 ON 期间将两者都包装在 Date() 中,因为没有它我得到了相同的查询结果,我希望这是由于无效的日期比较。

我的目标是将数据移动到新数据库。我没有创建旧的,但最初的数据库设计者将他们的查询配置为检查这两个表之间的日期。是的,inventory表中可以有多个取货日期相同的记录,但inventory是实际在手的库存。

这是collections表中的数据样本,collection_date是2045-04-16(别问,不是我的数据)...

2152   271   AN   3137   2045-04-16    6972   172   XX   ok+   50   3   45   2015-04-20 08:14:02   2015-04-20 03:14:01   NULL   jenna
 701   237   AN   2996   2017-07-21   18996    25   IO   ISR    0   0    0   2017-07-21 10:51:48   2017-07-21 05:51:47   NULL   michael
5633   271   AN   3817   2017-07-20   19004    47   R    ok    50   3    8   2017-07-21 11:11:52   2017-07-21 06:11:52   NULL   Megan
5634   271   AN   3818   2017-07-20   19002    52   M    ok    45   3    8   2017-07-21 11:05:06   2017-07-21 06:05:06   NULL   Megan

下面是库存表中的数据示例,1901-04-29 是库存收集日期。再次不要询问日期,而不是我的数据,我只是想将其移至新系统。

32711   159   5L Blazin View 1635-235x   10874   154   AR    207   1901-04-29   13   1    2   2014-02-10 16:04:59   2014-02-10 04:04:59   NULL, 
32712   114   5L Blazin View 1635-235x   10874   154   AR    207   1901-04-30   13   1   20   2014-02-10 16:04:59   2014-02-10 04:04:59   NULL, 
32713   121   5L Blazin View 1635-235x   10874   154   AR    207   1901-05-01   13   1   25   2014-02-10 16:04:59   2014-02-10 04:04:59   NULL, 
32714   130   5L Destination 893-6215    10874    99   AR   5602   1902-01-27    8   1   26   2016-04-21 06:24:31   2014-02-10 04:04:59   karla   
32715    45   5L Hobo Design 273-7047    10874    99   AR   6248   1900-07-31    5   1   34   2014-02-10 16:04:59   2014-02-10 04:04:59   NULL, 
32716    50   5L Hobo Design 273-7047    10874    99   AR   6248   1902-01-28    6   4   14   2014-02-10 16:04:59   2014-02-10 04:04:59   NULL, 
32717     1   5L Norse Design 673-5035   10874    75   AR    342   1900-05-31    7   1    2   2014-02-10 16:04:59   2014-02-10 04:04:59   NULL, 

了解如何停止指数返回结果。我知道左外连接可以返回比左表更多的行,但我不知道这种类型的连接在哪里可以返回比表中最大记录数多 20 倍的记录。这些结果大大超过了两个表中大约 36k 行的总和。

预期的结果是简单地将新的 collections.collectionId 连接到 inventory 表,这样我就可以删除当前系统中的日期关系。我希望返回具有关联 collectionId 的 20,867 条库存记录。

最佳答案

如果您仅使用提交日期连接表格,如果您在表 A 中有 5 条日期为 X 的记录,在表 B 中有 20 条记录具有相同的日期 X。您的查询结果将为 5 x 20 = 100

使用 date() 函数返回日期或日期时间表达式的日期部分。

我将尝试用一个例子来解释:

table_A
--------
nameA, date
a1, 2017-11-01
a2, 2017-11-01

table_B
-------
nameB, date
b1, 2017-11-01
b2, 2017-11-01

如果您使用查询中使用的类似连接在 B 上连接 A:

select nameA,nameB from table_A left join table_B on Date(table_A) = Date(table_B)

you will have:
a1, b1 -> Date(2017-11-01) is equal to Date(2017-11-01)
a1, b2 -> Date(2017-11-01) is equal to Date(2017-11-01)
a2, b1 -> Date(2017-11-01) is equal to Date(2017-11-01)
a2, b2 -> Date(2017-11-01) is equal to Date(2017-11-01)

请记住,在连接中使用 Date() 公式,您的数据库引擎将被迫不使用索引。那么这是一种非常糟糕且缓慢的数据查询方式。

关于mysql - 为什么 MySql LEFT OUTER JOIN 返回 20 倍以上的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46260822/

相关文章:

mysql - 使用 JOIN、UNION 和 ORDER BY 的子查询、使用临时、使用文件排序

php - 在 php 中使用 "ORDER BY"查询未提供准确的结果

php - 根据电子邮件地址更新 Mysql 列字段

sql - 如何在SQL Server中的两个SELECT语句之间执行LEFT JOIN?

c# - SQL:如何在添加额外条目的同时将列移动到另一个表中?

mysql - Yii2 使用左连接将 SqlDataProvider 转换为 ActiveDataProvider

php - 使用 MySQL INTO OUTFILE 不写入 tmp

sql - PostgreSQL 选择两列自引用

ms-access - 连接表并显示表一张表上的所有行,但当我在其中使用条件时不显示

MYSQL - 每 5 分钟获取一次数据,并对前几行求和以显示聚合数据