简体   繁体   English

从SQL查询结果中获取计数

[英]get count from sql query result

I have such query 我有这样的查询

SELECT `id`
FROM (`TABLE`)
WHERE date(FROM_UNIXTIME(time)) >= '2013-10-28'
GROUP BY `last`
ORDER BY `id_s` DESC

the result is: 结果是:

| | id | id |

9 9
1 1个
1 1个
9 9
50 50
3 3
1 1个

My question is how to count how many times repeating number 9 in result? 我的问题是如何计算结果重复9的次数?

So, the final result must be one row with number 2 因此,最终结果必须是数字2的一行

Somebody have idea how possible to do it? 有人知道怎么做?

You would use count(*) : 您将使用count(*)

SELECT count(distinct last)
FROM (TABLE)
WHERE date(FROM_UNIXTIME(time)) >= '2013-10-28' AND
      id = 9 ;

I'm not sure what the group by really does, so it might be sufficient to do: 我不确定该group by实际工作,所以这样做可能就足够了:

SELECT count(*)
FROM (TABLE)
WHERE date(FROM_UNIXTIME(time)) >= '2013-10-28' AND
      id = 9 ;

You could just use the result set that you already have, select id=9 from it and count the rows: 您可以只使用已有的结果集,从中选择id = 9并计算行数:

SELECT COUNT(*)
FROM (
  --your current query here
) WHERE `id`=9
GROUP BY `id`

There might be a better way of doing this in your situation, but without knowing your table structure and maybe seeing some example data, this is hard to tell. 在您遇到的情况下,可能会有更好的方法来执行此操作,但是在不了解表结构和可能看不到示例数据的情况下,这很难分辨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM