2017-09-17 36 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

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