日期:2014-05-18  浏览次数:20556 次

求个SQL语句或效率稍高一点的其它解决方案,我脑子转不过弯
现有两张表,用与处理消息中心消息会话。
message_sessions,是消息会话,其中包括该消息会话中包含的成员,对应于message_session_members表,外键session_id引用message_sessions.id。

现想求一SQL语句,查询一个消息会话中包含的成员和我传入的几个ID是否一致,并且数量也要一致。
比如message_sessions.id=1时,该会话有三个成员57,58,59,那么当我传入的id是57,58,59时才列出结果,当传入的id是57,58或57,58,59,60时,不列出结果

create table `message_sessions` (
`id` int ,
`message_count` int ,
`member_count` int
); 
insert into `message_sessions` (`id`, `message_count`, `member_count`) values('1','0','2');
insert into `message_sessions` (`id`, `message_count`, `member_count`) values('2','0','3');
insert into `message_sessions` (`id`, `message_count`, `member_count`) values('3','0','2');
insert into `message_sessions` (`id`, `message_count`, `member_count`) values('4','0','2');

create table `message_session_members` (
`session_id` int,
`user_id` int
); 
insert into `message_session_members` (`session_id`, `user_id`) values('1','57');
insert into `message_session_members` (`session_id`, `user_id`) values('1','58');
insert into `message_session_members` (`session_id`, `user_id`) values('1','59');
insert into `message_session_members` (`session_id`, `user_id`) values('2','57');
insert into `message_session_members` (`session_id`, `user_id`) values('2','120');
insert into `message_session_members` (`session_id`, `user_id`) values('2','121');
insert into `message_session_members` (`session_id`, `user_id`) values('3','58');
insert into `message_session_members` (`session_id`, `user_id`) values('3','59');
insert into `message_session_members` (`session_id`, `user_id`) values('4','57');
insert into `message_session_members` (`session_id`, `user_id`) values('4','58');




------解决方案--------------------
SQL code

create table message_session_members (
session_id int,
[user_id] int
);  
insert into message_session_members (session_id, [user_id]) values('1','57');
insert into message_session_members (session_id, [user_id]) values('1','58');
insert into message_session_members (session_id, [user_id]) values('1','59');
insert into message_session_members (session_id, [user_id]) values('2','57');
insert into message_session_members (session_id, [user_id]) values('2','120');
insert into message_session_members (session_id, [user_id]) values('2','121');
insert into message_session_members (session_id, [user_id]) values('3','58');
insert into message_session_members (session_id, [user_id]) values('3','59');
insert into message_session_members (session_id, [user_id]) values('4','57');
insert into message_session_members (session_id, [user_id]) values('4','58');
go
create proc getmembers
(
@session_id int,
@user_id varchar(30)
)
as
begin
    declare @sql varchar(8000) set @sql=''
    select @sql=@sql+ltrim([user_id])+',' from message_session_members 
    where session_id=@session_id order by [user_id]
    if(@user_id+','=@sql)
    select * from message_session_members where session_id=@session_id
end

exec getmembers 1,'57,58,59'
/*
session_id  user_id
----------- -----------
1           57
1           58
1           59
*/
exec getmembers 1,'57,58,59,60' --无结果
exec getmembers 1,'57,58' --无结果