2015-11-02 31 views
-2

我有兩個表,quotationcomparitive加入兩個表並顯示只有特定條件的記錄

quotation具有字段:tender_id, supplier_name

comparitive具有字段:tender_id, sup_name,make,shelf_life,datasheet,coc

現在我想的是,我需要它連接這兩個表並顯示記錄,其中quotation.tender_id=comparitive.tender_id and comparitive.tender_id=$tender_id and comparitive.sup_name IN quotation.supplier_name查詢。

我該如何做到這一點?我嘗試過不同的方式,但期望的輸出不會到來。

這是我試過的。

SELECT comparitive_statement1.sup_name 
, comparitive_statement1.tender_id 
, comparitive_statement1.coc 
, comparitive_statement1.shelf_life 
, comparitive_statement1.make 
, comparitive_statement1.datasheet 
, quotation_items.supplier_name 
, quotation_items.tender_id 
FROM comparitive_statement1 
, quotation_items 
WHERE comparitive_statement1.tender_id = quotation_items.tender_id 
AND quotation_items.tender_id='$tender_id' 
and quotation_items.supplier_name = comparitive_statement1.sup_name 
group by quotation_items.supplier_name 
+2

向我們展示您嘗試過的。 – Epodax

+0

我已經添加了我所嘗試過的。 –

+0

使用內部聯接您可以訪問期望的數據[link](http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/) – PRANAV

回答

0
SELECT cs.sup_name 
, cs.tender_id 
, cs.coc 
, cs.shelf_life 
, cs.make 
, cs.datasheet 
, q.supplier_name 
FROM comparitive_statement1 cs 
LEFT JOIN quatation_items q 
ON cs.tender_id = q.tender_id 
WHERE q.tender_id='$tender_id' 
AND q.supplier_name = cs.sup_name 
GROUP BY q.supplier_name 

該查詢應該工作。而且,在查詢中沒有要點comparitive_statement1.tender_idquotation_items.tender_id,因爲它們將是相同的。

相關問題