如果没有找到记录,则Mysql Complex查询生成所有具有计数零的表

时间:2017-06-28 05:52:47

标签: mysql phpmyadmin

我是一个复杂的查询。我冻结了位置:(。 这是我的桌子。 用户:

CREATE TABLE `users` (
  `id` tinyint(4) NOT NULL,
  `office_name` varchar(255) DEFAULT NULL,
  `district_id` int(10) DEFAULT NULL,
  `upazilla_id` int(10) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `mobile` varchar(11) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(100) NOT NULL,
  `type` varchar(10) NOT NULL,
  `del_status` tinyint(1) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `service` (
  `id` int(10) NOT NULL,
  `recipient_number` int(50) NOT NULL,
  `date` date NOT NULL,
  `office_id` int(10) NOT NULL,
  `del_status` tinyint(1) NOT NULL,
  `creation_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `service_list` (
  `id` int(3) NOT NULL,
  `service_name` varchar(50) NOT NULL,
  `del_status` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `service_transaction` (
  `id` int(10) NOT NULL,
  `service_transaction_id` int(50) NOT NULL,
  `service_id` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这里,service-id = service_transaction.service_transaction_id。

我打了个问题:

select users.id, users.office_name,service_transaction.service_id , COUNT(service_transaction.service_id) as service_total
from users
LEFT JOIN service on users.id = service.office_id
LEFT JOIN service_transaction on service_transaction.service_transaction_id = service.id
WHERE( users.del_status = 0 and users.type='agency')
GROUP BY users.office_name , users.id, service_transaction.service_id 

它返回:

id   office_name  Service_id  service_total
=============================================
2    Ctg Office      2             2
2    Ctg Office      3             4
2    Ctg Office      4             3
7    Dhaka Office   NULL           0

但我的欲望输出是:

id   office_name  Service_id  service_total
=============================================
2    Ctg Office      2             2
2    Ctg Office      3             4
2    Ctg Office      4             3
2    Ctg Office      5             0
2    Ctg Office      6             0

7    Dhaka Offc      2             0
7    Dhaka Offc      3             0
7    Dhaka Offc      4             0
7    Dhaka Offc      5             0
7    Dhaka Offc      6             0

这意味着,我必须显示每个办公室下的所有服务,如果没有服务,则计数应为零。

3 个答案:

答案 0 :(得分:1)

select temp.id office_id , temp.office_name, temp.svcic ,count(service_transaction.service_id) as service_total, count(recipient.id) as total_count from (select users.id, users.office_name ,service_list.id svcic from users,service_list WHERE users.del_status = 0 and users.type='agency' )  temp LEFT JOIN service on temp.id = service.office_id and service.del_status=0 LEFT JOIN service_transaction on service_transaction.service_id = temp.svcic and service_transaction.service_transaction_id=service.id right outer JOIN recipient on recipient.office_id = temp.id group by temp.id , temp.office_name, temp.svcic ORDER BY temp.id,temp.office_name

请尝试以上代码。 为了更具可读性,你应该保持意图

答案 1 :(得分:0)

select users.id, users.office_name,service_transaction.service_id , COUNT(service_transaction.service_id) as service_total
from users
JOIN service 
LEFT JOIN service_transaction on service_transaction.service_transaction_id = service.id
WHERE( users.del_status = 0 and users.type='agency')
GROUP BY users.office_name , users.id, service_transaction.service_id 

尝试以上查询。

希望这会对你有所帮助。

答案 2 :(得分:0)

如果没有解决,请尝试此操作,然后让我知道表服务和service_transaction表中的数据。

SELECT users.id,users.office_name,service_transaction.service_id,SUM(IF(service.id IS NULL,0,1))as service_total

来自用户

在users.id = service.office_id

上加入服务

在service_transaction.service_transaction_id = service.id上加入service_transaction

WHERE(users.del_status = 0和users.type ='agency')

GROUP BY users.office_name,users.id,service_transaction.service_id