我想针对某个人曾阅读过某条消息的某个订单进行对话。例如:
Order SentTOID ReadBy
A 111 55
A 55 55
A 111 89
B 111 89
C 111 55
C 55 55
D 111 99
D 99 99
结果
A 111 55
A 55 55
A 111 89
C 111 55
C 55 55
我的代码只会提取55次读取的所有案例,但我想要整个转换。
Order SentTOID ReadBy
A 111 55
A 55 55
C 111 55
C 55 55
使用的代码。
Select *
from conversation
where readby = 55.
答案 0 :(得分:1)
首先获取订单并应用IN
子句
declare @temp table
(Orders nvarchar(7), SentTOID int, ReadBy int)
insert into @temp values ('A',111,55)
insert into @temp values ('A',55 ,55)
insert into @temp values ('A',111,89)
insert into @temp values ('B',111,89)
insert into @temp values ('C',111,55)
insert into @temp values ('C',55 ,55)
insert into @temp values ('D',111,99)
insert into @temp values ('D',99 ,99)
select * from @temp
where Orders in (select Orders from @temp where ReadBy = 55)
RESULT