使用SQLServer逻辑地从多个表中获取记录

时间:2013-12-19 08:20:22

标签: sql database oracle sql-server-2008

我在获取记录时遇到问题。 虽然我正在尝试,但发现它太难以获得。 关于这个请帮帮我:: 实际上我的要求是从table1获取记录,其发送者和接收者userid = 5,如果所选结果的pkId存在于相应的记录中 table2为fkid,必须在table2中再次检查userId并使用Isdeleted = false

我想从table1获取记录,其中sender = 5或receiver = 5 然后我需要检查table1的pkId(如果它存在) table2作为fkId然后idDeleted应该是false但是 从table2检查,我有条件只搜索在哪里   table2.userid = 5

我试过这个但没有工作

 select distinct pkId ,  Message,  data  from  table1
      left outer join  table2 on 
    table2.fkId =table1.pkMessageId
    where ((table1.sender ='5' or  table1.receiver='5' )
    or table2.userid='5') and table2.isDeleted=0

table1
pkId     sender       receiver      Message    data         date

1            2                  5                 M1              D1           blah_Blah
2            2                  5                 M2              D2          blah_Blah
3            5                  7                 M3              D3          blah_Blah
4            5                  2                 M4              D4          blah_Blah
5            5                  2                 M5              D4          blah_Blah
table2
Id         fkId             userid             isDeleted

1          1                    5                    true
2          1                    5                    false
3          2                    5                   false
4          2                     2                  false
5          3                    2                   false
6          4                    2                   true
7          1                    2                   true
8          2                   2                    false

5 个答案:

答案 0 :(得分:0)

试试这个:

Select distinct pkId ,  Message,  data  from  table1
Where (sender = 5 Or receiver = 5) and pkId in
(
   Select fkid From table 
   Where isDeleted=0
)

答案 1 :(得分:0)

我认为您的问题是您使用senderreceiver相同的表格,您需要两次加入同一张桌子...

SELECT * FROM Table1
INNER JOIN Table2 Sender
ON Table1.Sender = Sender.userid
INNER JOIN Table2 Receiver
ON Table1.Receiver = Receiver.userid
WHERE
    Receiver.isDeleted = 0
AND
    Sender.isDeleted = 0
AND
( 
    Receiver.userid = 5
    OR 
    Sender.userid = 5
)

从您的描述中您所说的是

SELECT 
    T1.* 
FROM 
    Table1 T1
INNER JOIN 
    Table2 T2
ON 
    T1.pkId = T2.fkId
WHERE 
    (T1.Sender = 5 OR T1.Receiver = 5)
AND
    T2.isDeleted = 0
AND 
    T2.userid = 5

答案 2 :(得分:0)

你应该能够避免使用EXISTS语句来区分

select pkId, Message, data 
from  table1
where exists ( select 1
               from table2 
               where table2.fkId = table1.pkMessageId
               and table2.isDeleted=0
               and table2.userid='5' )
and   (sender ='5' or  receiver='5' )

这假设了一些事情:

  • table2.fkId = table1.pkMessageId是加入两个表的正确方法(示例数据中缺少pkMessageId)
  • 您不希望table1.sender ='5'或table1.receiver ='5'或table2.userid ='5'

如果你想要OR,OR,OR,这可能就是你想要的:

select pkId, Message, data 
from  table1
where exists ( select 1
               from table2 
               where table2.fkId = table1.pkMessageId
               and table2.isDeleted = 0
               and table2.userid='5' )
or    sender ='5'
or    receiver='5' 

答案 3 :(得分:0)

我将逐步完成您的新版本的要求:

  

实际上,我的要求是从table1获取记录,其发送者和接收者userid = 5,如果所选结果的pkId作为fkid存在于table2中,则必须在table2中再次检查userId并使用Isdeleted = false < / p>

我将逐步采取这一步。

  

来自table1的记录,其发件人和接收者userid = 5

这很简单,在您的示例数据中,没有这样的记录。 现在,我假设你的意思是

  

来自table1的记录,其发件人或接收者用户ID = 5

这导致了我们查询的第一部分:

SELECT * 
  FROM Table1 t1
 WHERE t1.sender = 5
    OR t1.receiver = 5

然后,我们走得更远:

  

以及相应的记录,如果所选结果的pkId作为fkid存在于table2中

因此,我们 LEFT JOIN 表2匹配键:

   SELECT * 
     FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.pkID = t2.fkId
    WHERE t1.sender = 5
       OR t1.receiver = 5

几乎就在那里:

  

并且必须使用Isdeleted = false

在table2中再次检查userId

假设如果满足此条件,您只想从table2添加额外数据,但如果条件不满足,您仍然需要来自table1的数据。为了实现这一点,我将把这个条件置于JOIN条件中,而不是在eWHERE子句中。在where子句中,如果不满足条件,则将删除整个记录,而不仅仅是table2中的数据。

   SELECT * 
     FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.pkID = t2.fkId
                   AND t2.IsDeleted = 0
    WHERE t1.sender = 5 
       OR t1.receiver = 5

现在,这看起来很像你已经创建的,但我不知道为什么它不会给你想要的结果。因此,如果这不能给您预期或预期的结果,请告诉我们出了什么问题:)

答案 4 :(得分:-1)

--Try Like This..
SELECT * FROM Table1
Where ( Sender = 5  
OR Reciever = 5 )
AND PKID  in ( Select FKID From Table2 Where IsDeleted = 0)

-- It would be easy if u can provide the table structures also...