2014-01-21 200 views
0

我有一個fb_requests表。 enter image description hereSql select count with count

我想選擇基於accept_status的game_selected列,如果accept_status數爲< 4,我想選擇那些行。努力工作,請幫我解決這個問題。

這是我創建嘗試這種代碼表的代碼

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', 
       `created_date` datetime DEFAULT NULL,         
       `modified_date` datetime DEFAULT NULL,         
       PRIMARY KEY (`id`)              
      ) ENGINE=MyISAM AUTO_INCREMENT=190 DEFAULT CHARSET=latin1  

。我知道它在語法上不正確,但嘗試過。

Select 
    game_selected 
from 
    fb_requests 
where 
    user_id = 17 
    && 
    (
     count(accept_status =1) < 4 
    ) 
group by 
    game_selected; 

在此先感謝。

+1

你想看什麼?請描述你想要達到的目標。 – PeterRing

+0

作爲一般規則,CREATE TABLE代碼(發佈爲文本,省略了不相關的列)和一些帶示例數據的INSERT INTO查詢比數據圖片更有用,因爲它允許潛在的回答者真正測試查詢。 –

+0

如果accept_status計數<4,我想選擇基於accept_status的game_selected列,您想要accept_status計數或accept_status = 1的行數? – grasshopper

回答

1

您應該使用具有聚合函數的SQL語句
試試這個

Select game_selected from fb_requests 
where user_id=17 
group by game_selected 
having count(accept_status)<4; 
+0

感謝您的幫助,它的工作,有沒有什麼辦法可以只計數(accept_status = 1) – Dibish

+0

@Dibish是的,你可以修改WHERE語句是這樣的user_id = 17 AND accept_status = 1 –

1

試試這個

Select game_selected from fb_requests 
    where user_id=17 
    group by game_selected 
    having count(accept_status)<4; 

更新:

Select game_selected from fb_requests 
    where user_id=17 and accept_status=1 
    group by game_selected 
    having count(accept_status)<4; 
+0

非常感謝你的快速回復,我如何選擇accept_status = 1記錄與相同的查詢?我的意思是我想要accept_status = 1的計數只有 – Dibish

+0

嘗試更新部分。 –

1

我認爲它是因爲聚合f聯合,試試這個。

Select game_selected 
from fb_requests 
where user_id=17 
group by game_selected 
HAVING COUNT(accept_status) < 4; 
+0

非常感謝您的快速回復,我如何使用相同的查詢選擇accept_status = 1條記錄?我的意思是我想要只接受accept_status = 1的計數 – Dibish

1

請嘗試以下查詢按您的要求

Select game_selected,count(accept_status) as as_cnt 
    from 
     fb_requests 
    where 
     user_id = 17 
    group by 
     game_selected 
having as_cnt < 4; 

OR

下面的查詢只accept_status標記爲 '1'

Select 
     game_selected,sum(accept_status) as as_cnt 
    from 
     fb_requests 
    where 
      user_id = 17 
     group by 
      game_selected 
    having as_cnt < 4; 
+0

這不起作用。 Where子句只能使用現有列的現有列或組合(但不能合併)。好吧,我看到你編輯這個,並用它來代替。 – grasshopper

+0

是的,我改變了 –

+0

@YograjSudewad Sudewad非常感謝您的代碼,在您的第二個查詢中,我沒有收到accept_status = 1的計數 – Dibish

0

從fb_requests中選擇game_selected,其中user_id = 17 group by game_selected having count(accept_status)< 4;

+0

非常感謝你的快速回復,怎麼可以我選擇相同的查詢accept_status = 1記錄?我的意思是我想只接受accept_status = 1的計數 – Dibish

1

如下嘗試:

Select fbr1.game_selected,fbr1.* from fb_requests fbr1 JOIN 
(select id,accept_status from fb_requests where accept_status =1 limit 4) fbr2 
ON fbr1.id=fbr2.id where fbr1.user_id = 17; 

讓我知道,如果你是不是在找結果從上面的查詢獲取。

+0

感謝您的回答。沒有嘗試過。我會嘗試一下,讓你知道。下面的答案爲我工作,這就是爲什麼我沒有試過這個答案。謝謝您的幫助 – Dibish