如何从表一中选择一列,同时从表二中传递一列值?

时间:2014-09-24 07:31:58

标签: mysql sql join subquery

我有两张牌partner_routing和伙伴。我试图从partner_routing表中选择routingId,同时尝试满足两个表中的伙伴ID相同且来自partner_info id的客户端ID将作为输入的条件

SELECT partnerRouting.routingId 
FROM partner_routing partnerRouting, partner_info partner
WHERE partner.partner_id=partnerRouting.partnerId 
  AND partner.clientId=1
  AND partnerRouting.deleted = 0;

SELECT routingId 
FROM partner_routing partnerRouting, partner_info partner
WHERE partner.partner_id = partnerRouting.partnerId 
  AND partnerRouting.deleted = 0
  AND partner.clientId IN (
   SELECT clientId FROM partner_info partner WHERE partner.clientId = 1
  );

SELECT routingId
FROM partner_routing partnerRouting
  INNER JOIN partner_info partner ON partner.partner_id = partnerRouting.partnerId
WHERE partnerRouting.deleted = 0
  AND partner.clientId = 1;

我没有得到上述所需的结果...如果我删除partner.clientId = 1部分然后我得到结果,但我需要根据partner_info表中的客户端ID过滤结果。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

使用INNER JOIN进行此

SELECT partner_routing.routingId 
FROM partner_routing INNER JOIN partner_info 
ON partner_routing.partner_id = partner_info.partner_id
WHERE partner_info.clientId=1

您使用错误的INNER JOIN语法,请使用以下语法

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

检查http://www.w3schools.com/sql/sql_join_inner.asp以获取更多信息。