连接两个表并显示仅具有特定条件的记录

时间:2015-11-02 10:43:09

标签: php mysql

我有两个表,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

1 个答案:

答案 0 :(得分: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,因为它们是相同的。