我有这样的表结构
CREATE TABLE `fb_requests`
(
`id` int(60) NOT NULL AUTO_INCREMENT,
`user_id` int(60) DEFAULT NULL,
`fb_user_id` varchar(255) DEFAULT NULL,
`request_id` varchar(255) DEFAULT NULL,
`game_selected` int(60) DEFAULT NULL,
`accept_status` int(60) DEFAULT NULL COMMENT '0 = pending 1 = accept', 2 = reject
`created_date` datetime DEFAULT NULL,
`modified_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=187 DEFAULT CHARSET=latin1
有accept_status
列包含0,1,2状态
为了获得所需的数据,我正在编写三个查询,首先查询获取接受状态= 1行
select
fb_user_id, accept_status
from
fb_requests
where
user_id = 17
and accept_status = 1
and game_selected = 2
group by
fb_user_id;
第二次查询获取接受状态= 2
select
fb_user_id, accept_status
from
fb_requests
where
user_id = 17
and accept_status = 2
and game_selected = 2
group by
fb_user_id;
在第三个查询中,我需要获取不在先前查询中的行,因此我使用了not in
子句。
select
fb_user_id, accept_status
from
fb_requests
where
user_id = 17
and accept_status = 0
and game_selected = 5
and fb_user_id not in (select fb_user_id
from fb_requests
where user_id = 17
and accept_status = 2
and game_selected = 5
and fb_user_id not in (select fb_user_id
from fb_requests
where user_id = 17
and accept_status = 1
and game_selected = 5)
)
group by
fb_user_id
但是在第三次查询的情况下有些事情是不对的,我没有得到理想的结果。我不确定这是两个不是条款的结合方式。我需要在这些查询之间获得一个没有重复fb_user_id的数组。
我期待这种类型的查询首先采取
$query = select fb_user_id,accept_status from fb_requests where
user_id= 17 and accept_status=0 and game_selected=2 and
fb_user_id not in (select fb_user_id from fb_requests where user_id= 17
and accept_status=1 and game_selected=2 ) group by fb_user_id
and then $query not in select fb_user_id from fb_requests where user_id= 17
and accept_status=2 and game_selected=2
请帮我找一个解决方案。谢谢
答案 0 :(得分:1)
试试这个:
select
fb_user_id, accept_status
from
fb_requests
where
user_id = 17
and accept_status = 0
and game_selected = 5
and fb_user_id not in (select fb_user_id
from fb_requests
where user_id = 17
and (accept_status = 2 or accept_status = 1)
and game_selected = 5)
group by
fb_user_id, accept_status