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
Mysql 在缺少零计数的多个表上计数_Mysql_Database - Fatal编程技术网

Mysql 在缺少零计数的多个表上计数

Mysql 在缺少零计数的多个表上计数,mysql,database,Mysql,Database,我正在运行此查询以返回计数小于0的数据。它可以正常工作,直到计数大于0且小于50。但当count变为0时,它不会返回数据。计数由优惠券`.`status定义。计数为零时,优惠券表中没有状态为1的数据。这就产生了问题,因为它忽略了整行 SELECT count(*) AS count, clients.title, plans.name FROM `coupons` INNER JOIN `clients` ON `coupons`.`client_id` = `clients`.`id

我正在运行此查询以返回计数小于0的数据。它可以正常工作,直到计数大于0且小于50。但当count变为0时,它不会返回数据。计数由
优惠券`.`status
定义。计数为零时,
优惠券
表中没有
状态为1的数据。这就产生了问题,因为它忽略了整行

SELECT count(*) AS count, clients.title, plans.name 
FROM `coupons`
    INNER JOIN `clients` ON `coupons`.`client_id` = `clients`.`id`  
    INNER JOIN `plans` ON `coupons`.`plan_id` = `plans`.`id`
WHERE `coupons`.`status` = 1
GROUP BY `coupons`.`client_id`, `coupons`.`plan_id` 
HAVING count < 50
基本上,一个客户机可以有多个计划,一个计划可以属于多个客户机

优惠券表存储可分配给客户的预定义优惠券。未分配优惠券的状态为
0
,而已分配优惠券的状态为
1

在这里,我尝试获取未分配的客户、计划优惠券计数,其中计数小于50或计数已达到0

比如说,


如果优惠券表有10行client_id=1和plan_id=1,状态为1,那么它应该返回count为10,但是当该表有0行client_id=1和plan_id=1,状态为1时,它不会在上述查询中返回任何内容。

对于内部联接,查询不会返回任何“零”计数

如果要返回“零”计数,则需要在某个位置进行外部联接

但不清楚你到底在数什么

假设您试图获得的是来自
优惠券的行数
,对于
计划
客户
的行的每种可能组合,您可以执行以下操作:

SELECT COUNT(`coupons`.`client_id`) AS `count`
     , clients.title
     , plans.name
  FROM `plans`
 CROSS 
  JOIN `clients`
  LEFT
  JOIN `coupons`
    ON `coupons`.`client_id` = `clients`.`id`
   AND `coupons`.`plan_id`   = `plans`.`id`  
   AND `coupons`.`status`    = 1
 GROUP
    BY `clients`.`id`
     , `plans`.`id`
HAVING `count` < 50
SELECT SUM( `coupons`.`status` = 1 ) AS `count`
     , clients.title
     , plans.name
  FROM `coupons`

  JOIN `plans`
    ON `plans`.`id` = `coupons`.`plan_id`

  JOIN `clients`
    ON `clients`.`id` = `coupons`.`client_id`

 GROUP
    BY `clients`.`id`
     , `plans`.`id`

HAVING `count` < 50
您还可以使用其他表达式来获取条件“count”。比如说

SELECT COUNT( IF(`coupons`.`status`=1, 1, NULL) ) AS `count` 

或者,对于更符合ANSI标准的方法

SELECT SUM( CASE WHEN `coupons`.`status` = 1 THEN 1 ELSE 0 END ) AS `count`

谢谢大家的投入,这很有效

select 
    sum(CASE WHEN `coupons`.`status` = 1 THEN 1 ELSE 0 END) as count,     
    clients.title, 
    plans.name 
from 
    `clients` 
left join 
    `coupons` 
on 
    `coupons`.`client_id` = `clients`.`id` 
left join 
    `plans` 
on 
    `coupons`.`plan_id` = `plans`.`id` 
group by 
    `coupons`.`client_id`, 
    `coupons`.`plan_id` 
having 
    count < 50
选择
总和(当“优惠券”。“状态”=1,则为1,否则为0结束)作为计数,
客户名称,
计划名称
从…起
`客户`
左连接
`优惠券
在…上
`优惠券`.`client\u id`=`clients`.`id`
左连接
`计划`
在…上
`优惠券`.`plan\u id`=`plan`.`id`
分组
`优惠券`.`client_id`,
`优惠券`.`plan\u id`
有
计数<50

我不知道您的具体目标是什么,但我想您可以使用左连接。您可以从clients表开始,在优惠券上左连接(现在您得到一个计数为零但值为空的结果行),它应该根据您的where条件正确。请用简单的英语描述您的目标,不要引用特定的列,并提供具有预期结果的示例数据。例如,您是否希望统计每个客户使用的所有优惠券以及相关计划?我希望获得计数小于50(包括0)的所有优惠券。通过这种方式,我可以获得计数小于50的所有优惠券,但不能获得计数为0的优惠券。尽管优惠券,但左加入客户端和左加入计划。状态=1可能仍会消除一些优惠券。更新问题。您的答案也返回0个计数记录,除了它得到所有客户/所有计划。我希望该组合基于优惠券表进行限制,因为该表包含客户id、计划id组合。
SELECT SUM( CASE WHEN `coupons`.`status` = 1 THEN 1 ELSE 0 END ) AS `count`
select 
    sum(CASE WHEN `coupons`.`status` = 1 THEN 1 ELSE 0 END) as count,     
    clients.title, 
    plans.name 
from 
    `clients` 
left join 
    `coupons` 
on 
    `coupons`.`client_id` = `clients`.`id` 
left join 
    `plans` 
on 
    `coupons`.`plan_id` = `plans`.`id` 
group by 
    `coupons`.`client_id`, 
    `coupons`.`plan_id` 
having 
    count < 50