加入3桌不能正常工作

时间:2013-02-14 05:47:24

标签: sql sql-server sql-server-2008

我正在尝试根据业务和订阅者ID获取成员。但这不能正常工作。请帮我解决这个问题。

SELECT distinct m.Fname,m.Created,m.[User_Name],b.Business_Name 
FROM dbo.Members m,dbo.Business b,dbo.Assign_Business a 
WHERE  m.Subcriber_id=a.Subscriber_id 
AND  a.Business_id=b.id 
AND b.Subcriber_id=a.Subscriber_id 
AND  a.Business_id='6' AND a.Subscriber_id='1'

4 个答案:

答案 0 :(得分:1)

试试这个..

select distinct m.Fname,m.Created,m.[User_Name],b.Business_Name 
from dbo.Members m
left join dbo.Assign_Business a on m.Subcriber_id=a.Subscriber_id
left join dbo.Business b on a.Business_id=b.id 
 where a.Business_id='6' and a.Subscriber_id='1' and  b.Subcriber_id=a.Subscriber_id 

答案 1 :(得分:1)

第一步,干净地格式化代码。这将有助于调试。

SELECT 
  distinct m.Fname,
           m.Created,
           m.[User_Name],
           b.Business_Name 
FROM 
  dbo.Members as M 
  inner join dbo.Assign_Business as A on M.Subscriber_Id = A.Subscriber_id
  inner join dbo.Business as B on B.id = A.Business_id and B.Subscriber_id = A.Subscriber_id 
WHERE
  A.Business_ID = '6'
  and A.Subscriber_id = '1'

答案 2 :(得分:1)

如果没有看到你的桌子的DDL就很难说,但你可以尝试

SELECT DISTINCT 
       m.Fname,m.Created,m.[User_Name],b.Business_Name 
  FROM dbo.Business b INNER JOIN 
       dbo.Assign_Business a ON a.Business_id=b.id AND 
                                a.Subscriber_id=b.Subcriber_id INNER JOIN
       dbo.Members m ON m.Subcriber_id=a.Subscriber_id
 WHERE a.Business_id=6 AND 
       a.Subscriber_id=1

答案 3 :(得分:1)

尝试以下方法:

SELECT DISTINCT
   m.Fname,
   m.Created,
   m.[User_Name],
   b.Business_Name 
FROM dbo.Members m
   INNER JOIN dbo.Assign_Business a ON m.Subcriber_id=a.Subscriber_id
   INNER JOIN dbo.Business b ON a.Business_id=b.id AND b.Subcriber_id=a.Subscriber_id 
WHERE  
   a.Business_id='6' AND a.Subscriber_id='1'