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