Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 一个表中的多个计数函数_Mysql_Sql - Fatal编程技术网

Mysql 一个表中的多个计数函数

Mysql 一个表中的多个计数函数,mysql,sql,Mysql,Sql,我希望在同一个表上放置多个计数,并且输出应该仅在一个表中使用“name” select count(select count(TXN) from sale1 where date(SALEDATE)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))) as c1, count(select count(TXN) from sale1 where RESPONSE='Y' date(SALEDATE)=DATE(DATE_SUB(NOW(), INTERVAL 1 DA

我希望在同一个表上放置多个计数,并且输出应该仅在一个表中使用“name”

select count(select count(TXN)
from sale1
where date(SALEDATE)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))) as c1, 
count(select count(TXN) from sale1 where RESPONSE='Y' date(SALEDATE)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))) as c2
 FROM sale1;
请试试这个 可计数的TXN
它将给出不同的计数,即重复字段,它将计数一次。

根据我从您的问题中了解到的情况,您希望对同一表格的数据进行多次计数,我假设这两种情况:

select count(TXN)
from sale1
where date(SALEDATE)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY));

您的查询似乎不正确,您似乎希望在新查询上使用:countselect。。。但不能对当前表的行列表进行计数

您可能希望这样做,以便在同一输出中具有不同的计数列:

select sum(case when response='y' then 1 else 0 end) as yes_count,
sum(case when date(SALEDATE)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY) then 1 else 0 end) as day_interval_count
from sale1 
where response='y'
在这里,我根据行值生成1或0,然后使用sum进行计数

另一种方法是进行多个查询,您可以将这些查询组合起来创建两行:

select count(x) as count
from sale1 
where response='y'
union
select count(x) as count
from sale1 
where response='n' 

试图格式化SQL并不能使意图更加清晰。样本数据和所需结果将有所帮助。从sale中选择countcase when dateSALEDATE=DATEDATE\u SUBNOW,INTERVAL 1天然后1其他0结束为count1,countcase when RESPONSE='Y'和dateSALEGDATE=DATEDATE\u SUBNOW,INTERVAL 1天然后1其他0结束为count2;我试过了,但它是计算整个数据,我只想计算countTXN,我试着改进一下这个问题,countTXN的计数是多少?如果表中有100条记录,其中40条记录的TXN值为空,其他记录的TXN值为空,那么countTXN为40。那么这算什么?1,因为40只是一个数字?但这对于任何数字都是正确的,因此完全独立于表中的内容。那么:你到底想数什么?
select count(x) as count
from sale1 
where response='y'
union
select count(x) as count
from sale1 
where response='n'