在mysql查询中显示计数器

时间:2016-10-05 07:42:37

标签: mysql sql

我有下面给出的mysql查询。我使用了哪个计数器。如果我输入类别Id 1 3次然后计数器来了3这是正确的但是我希望如果我不输入那么不同的coloumn应该来NO。 输出应该是

      KU      Electrical  
Yes   6       2  
No    1       2  

在这个KU和Electrical是我的销售渠道名称。是表示KU的进口柜号和没有进入的没有进口的单位。请帮帮忙。我在挣扎

select 
  SalesChannel.name, 
  Transaction.category_id, 
  count(Transaction.category_id) as "count"
from outlets Outlet 
inner join transactions Transaction on Outlet.id = Transaction.outlet_id 
inner join sale_channels SalesChannel on SalesChannel.id = Outlet.sale_channel_id
group by Transaction.category_id;

下面是我使用的三个表

1)交易

CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(11) NOT NULL,
  `zone_id` int(11) NOT NULL,
  `state_id` int(11) NOT NULL,
  `city_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  `sub_category_id` int(11) NOT NULL,
  `brand_id` int(11) NOT NULL,
  `model_id` int(11) NOT NULL,
  `outlet_id` int(11) NOT NULL,
  `no_of_units` int(11) NOT NULL,
  `mop` decimal(10,2) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `transactions`
--

INSERT INTO `transactions` (`id`, `zone_id`, `state_id`, `city_id`, `category_id`, `sub_category_id`, `brand_id`, `model_id`, `outlet_id`, `no_of_units`, `mop`) VALUES
(1, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(2, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(3, 1, 1, 1, 1, 1, 1, 1, 1, 4, '2.00'),
(4, 2, 2, 2, 1, 1, 1, 1, 2, 4, '2.00');

2)网点

CREATE TABLE IF NOT EXISTS `outlets` (
`id` int(11) NOT NULL,
  `outlet_code` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `zone_id` int(11) NOT NULL,
  `state_id` int(11) NOT NULL,
  `city_id` int(11) NOT NULL,
  `sale_channel_id` int(11) NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `outlets`
--

INSERT INTO `outlets` (`id`, `outlet_code`, `name`, `zone_id`, `state_id`, `city_id`, `sale_channel_id`, `is_active`, `created`, `modified`) VALUES
(1, '1508', 'Ashok electricals', 2, 2, 2, 1, 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, '1233', 'vinayak electricals', 1, 1, 1, 2, 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');

3)sale_chennals

CREATE TABLE IF NOT EXISTS `sale_channels` (
`id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `sale_channels`
--

INSERT INTO `sale_channels` (`id`, `name`, `is_active`, `created`, `modified`) VALUES
(1, 'KU', 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, 'Electricals', 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');

SQL小提琴:http://sqlfiddle.com/#!9/3f497/1

1 个答案:

答案 0 :(得分:0)

您按类别进行分组。这意味着每个类别可以获得一个结果行。在每个行中,您都会显示计数和销售渠道名称。此销售渠道名称只是在任意选择的类别的记录中找到的名称之一。

我想你想要计算每个类别和销售渠道。因此,您的group by子句应为group by SalesChannel.name, Transaction.category_id

select 
  SalesChannel.name, 
  Transaction.category_id, 
  count(Transaction.category_id) as "count"
from outlets Outlet 
inner join transactions Transaction on Outlet.id = Transaction.outlet_id 
inner join sale_channels SalesChannel on SalesChannel.id = Outlet.sale_channel_id
group by SalesChannel.name, Transaction.category_id;

SQL小提琴:http://sqlfiddle.com/#!9/3f497/2

但是,此结果并未显示Electricals / category 2的条目,因为表中没有此组合的事务。如果要为此显示零计数,则必须首先创建完整的结果集(即通道和类别的所有组合,无论它们是否有事务)。然后你将外部加入交易:

select 
  sc.name, 
  c.id as category_id, 
  count(t.id) as "count"
from sale_channels sc
cross join categories c
left join outlets o on o.sale_channel_id = sc.id
left join transactions t on t.outlet_id = o.id and t.category_id = c.id
group by sc.name, c.id;

SQL小提琴:http://sqlfiddle.com/#!9/60e998/5