简体   繁体   English

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

[英]join two tables and show records only having specific conditions

I have two tables, quotation and comparitive .我有两个表, quotationcomparitive

Table quotation has fields: tender_id, supplier_namequotation有字段: tender_id, supplier_name

Table comparitive has fields: tender_id, sup_name,make,shelf_life,datasheet,coc comparitive表有字段: tender_id, sup_name,make,shelf_life,datasheet,coc

Now what I want is I need a query which joins these two tables and show records where quotation.tender_id=comparitive.tender_id and comparitive.tender_id=$tender_id and comparitive.sup_name IN quotation.supplier_name .现在我想要的是我需要一个连接这两个表并显示记录的查询,其中quotation.tender_id=comparitive.tender_id and comparitive.tender_id=$tender_id and comparitive.sup_name IN quotation.supplier_name

How can I achieve that?我怎样才能做到这一点? I have tried different ways but desired output is not coming.我尝试了不同的方法,但没有得到想要的输出。

This is what I have tried.这是我尝试过的。

 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
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

This query should work.这个查询应该可以工作。 Also, there is no point to have comparitive_statement1.tender_id and quotation_items.tender_id in the query as they will be identical.此外,没有一点有comparitive_statement1.tender_idquotation_items.tender_id查询,因为他们将是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM