2014-01-21 202 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