好吧,我在收到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有(至少)兩個收集記錄。那麼在那個日期之後應該選哪一個作爲庫存記錄?到目前爲止,兩個記錄都已加入,但您只需要其中一個。哪一個?你想在這裏應用什麼規則? –