2015-11-02 27 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,因为它们将是相同的。

相关问题