else未在mysql查询中运行

pzfprimi  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(274)

我有以下疑问。在这种情况下,我有是和否的情况。是的,正在访问,但其他部分不工作。请看一下这个。

SELECT SalesChannel.name , count(Transaction.category_id) as count, (case when (Transaction.no_of_units > 0 and Transaction.mop > 0) THEN 'yes' ELSE 'No' END) AS Is_Present 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

输出应如下所示

KU      Electrical  
Yes   6       2  
No    1       2

6是ku的计数器,yes表示ku的存在,同样no表示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 SalesChannel.name

下面是我用过的三张表。交易
CREATE TABLE IF NOT EXISTStransactions(idint(11) NOT NULL,zone_idint(11) NOT NULL,state_idint(11) NOT NULL,city_idint(11) NOT NULL,category_idint(11) NOT NULL,sub_category_idint(11) NOT NULL,brand_idint(11) NOT NULL,model_idint(11) NOT NULL,outlet_idint(11) NOT NULL,no_of_unitsint(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.销售额

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');
pn9klfpd

pn9klfpd1#

您正在聚合数据,以便为每个salesschannel.name获取一行。对于saleschannel.name,可能有一些事务记录的结果为“是”,而其他事务记录的结果为“否”,那么\u present应该是什么?
查询的另一个问题是销售渠道在一个表中。目前有两个,但有时可能有三四个或几千个。sql查询不会产生列数可变的结果。列必须事先知道。所以一个可能的结果可能是这样的:

Name          Yes   No
KU            6     1  
Electrical    2     2

因为你知道你只想要是或不是,不管有多少个频道。
查询:

select 
  sc.name,
  count(case when t.no_of_units > 0 and t.mop > 0 then 1 end) as yes,
  count(case when t.no_of_units <= 0 or t.mop <= 0 then 1 end) as no
from sale_channels sc
join outlet o on o.sale_channel_id = sc.id
join transactions t on t.outlet_id = o.id;
xxhby3vn

xxhby3vn2#

表中没有与else条件匹配的数据。您的条件是两个字段的表值中不匹配的“transaction.no\u of \u units>0 and transaction.mop>0”大于0。
否则,else条件正常。

相关问题