2017-03-27 136 views
0

在我database值我要選擇每construction_manual的MySQL,選擇,比較,從2個表

where storage_room.quantity > component.quantity

但是當我使用:

SELECT construction_manual.name 
FROM construction_manual cm, construction_manual_component cmc, component, storage_room 
WHERE cm.ID = cmc.construction_manual_ID 
AND cmc.component_ID = component.ID 
AND component.storage_room_ID = storage_room.ID 
AND storage_room.quantity > component.quantity 

這將選擇每construction_manual.name爲只要storage_room中的第一個組件具有足夠的數量。 所以我們說......

construction_manual.1 needs

component.A -> quantity 5 
component.B -> quantity 10 

,並在儲藏室有:

component.A -> quantity 6 
component.B -> quantity 0 

雖然沒有足夠的儲藏室,construction_manual.1將被選中。如何僅選擇那些存儲空間中有足夠組件的?


編輯:

在我的4個表下面的數據集。當我使用上面提到的查詢時,我會得到以下結果:database。但我不應該能夠構建一個表,因爲沒有足夠的wooden_plates(木板,而不是木質板OFC ... UPS)


+0

PLZ提供多一點的數據,以便它可以很容易編寫任何查詢 – Rams

+0

您在存儲50個螺釘。這大於組件(10)中的數量。螺釘(component_ID:3)在椅子(1)和表格(2)的手冊中,因此結果將顯示兩個施工手冊。 – JuveLeo1906

回答

0

我想你要問什麼是忽略或排除任何沒有足夠組件來滿足構建的施工手冊。如果是這樣的話,你可以這樣做下面的(左連接也做):

select distinct cm.name as construction_manual 
    from construction_manual cm 
    join construction_manual_component cmc 
    on cmc.construction_manual_ID = cm.ID 
    join component c 
    on c.ID = cmc.component_ID 
    join storage_room sr 
    on sr.ID = c.storage_room_ID 
where sr.quantity > c.quantity 
    and cm.ID not in (select cm.ID 
         from construction_manual cm 
         join construction_manual_component cmc 
         on cmc.construction_manual_ID = cm.ID 
         join component c 
         on c.ID = cmc.component_ID 
         join storage_room sr 
         on sr.ID = c.storage_room_ID 
         where sr.quantity < c.quantity);