【发布时间】:2014-02-07 02:12:01
【问题描述】:
我有一个这样的表结构
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
但是在第三次查询的情况下有些不对劲,我没有得到想要的结果。我不确定这是结合两个 not in 子句的方式。我需要在这些查询之间获取一个不重复 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
请帮助我找到解决方案。谢谢
【问题讨论】: