2017-09-17 101 views
1

好吧,我在收到400816行返回以下LEFT OUTER JOIN查询为什么MySql LEFT OUTER JOIN返回20倍的行数?

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`) 

库存表有20867条记录,收藏表有15326条记录。那么上述查询如何返回400,816条记录呢?

库存和集合中的collection_date表是MySql数据类型= DATE。我在打开日期()期间包装了两个,因为我得到的查询结果没有它,我希望这是由于无效的日期比较。

目标是将数据移动到新的数据库。我没有创建旧的,但原始的数据库设计师配置了他们的查询来检查这两个表之间的日期。是的,库存表中可以有多个具有相同采集日期的记录,但库存是实际的库存。

这是在集合表数据的样本,collection_date是2045年4月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连接到库存表,这样我就可以删除当前系统中的日期关系。我希望能通过关联的collectionId返回20,867个库存记录。

+1

您需要20,867个结果行,每个库存记录一个。我们可以看到,2017-07-20有(至少)两个收集记录。那么在那个日期之后应该选哪一个作为库存记录?到目前为止,两个记录都已加入,但您只需要其中一个。哪一个?你想在这里应用什么规则? –

回答

1

如果仅使用日期字段加入您的表格,如果您在tableA中有5个记录,并且日期X和tableB中的20个记录具有相同的日期X.您的查询结果将为5 x 20 = 100

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

我会尝试用一个例子来explay:

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,在您的查询连接使用:

选择NAMEA,nameB从TABLE_A左连接表-B上日期(TABLE_A)= 日期(表-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()公式时,数据库引擎被迫不使用索引。那么这是一个非常糟糕而且很慢的查询数据的方式。

+0

我可以看到并理解你的解释,但要做到这一点,他们将是日期时间列,他们是日期列。正如我在我的问题中说过的,我在Date(inventory.collection_date)= Date(collections.collection_date)之前尝试了inventory.collection_date = collections.collection_date,并且仍然返回了400k +行...除非您指示加入在日期之外工作on子句和使用额外的日期字段? – aallord

+0

如果仅使用提交日期加入您的表格,如果您在表格A中有5条记录,并且日期X和表格B中有20条记录的日期X相同,则您的查询结果将为5 x 20 = 100行 –

+0

我只是更新了我的答案,我希望现在更清楚:-) –