dongpangfu6322 2015-11-02 10:43
浏览 60

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

I have two tables, quotation and comparitive.

Table quotation has fields: tender_id, supplier_name

Table comparitive has fields: 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.

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
  • 写回答

1条回答 默认 最新

  • dqmq0654 2015-11-02 13:49
    关注
    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度