2014-02-07 71 views
0

我有一個像這樣SQL SELECT查詢未未條款

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 

請幫我找到一個解決方案。由於

回答

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 
+0

太謝謝你了埃裏克,你救了我life..its在所有測試的情況下工作..我必須做更多的測試...我會接受你的答案。再一次感謝你.. – Dibish