从具有union的多个表中选择计数相关行

时间:2015-12-02 02:03:16

标签: mysql sql

在这种情况下有三个表

4a和4b是主表

tbhari只有一个类别表

CREATE TABLE IF NOT EXISTS `4a` (
  `kd_hari` char(5),
  `kd_ruang` char(1)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `4a` (`kd_hari`,`kd_ruang`) VALUES
('a_Mon','0'),
('a_Mon','3'),
('a_Mon','P'),
('a_Mon','0'),
('b_Tue','0'),
('b_Tue','0'),
('b_Tue','P'),
('b_Tue','4'),
('c_Wed','0'),
('c_Wed','P'),
('c_Wed','2'),
('c_Wed','L'),
('d_Thu','0'),
('d_Thu','0'),
('d_Thu','3'),
('d_Thu','0'),
('e_Fri','0'),
('e_Fri','0'),
('e_Fri','0'),
('e_Fri','0');

CREATE TABLE IF NOT EXISTS `4b` (
  `kd_hari` char(5) DEFAULT NULL,
  `kd_ruang` char(1) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `4b` (`kd_hari`,`kd_ruang`) VALUES
('a_Mon','0'),
('a_Mon','4'),
('a_Mon','L'),
('a_Mon','0'),
('b_Tue','4'),
('b_Tue','0'),
('b_Tue','4'),
('b_Tue','0'),
('c_Wed','0'),
('c_Wed','3'),
('c_Wed','L'),
('c_Wed','0'),
('d_Thu','0'),
('d_Thu','0'),
('d_Thu','0'),
('d_Thu','0'),
('e_Fri','3'),
('e_Fri','L'),
('e_Fri','0'),
('e_Fri','0');

CREATE TABLE IF NOT EXISTS `tbhari` (
`kd_hari` char(5) DEFAULT NULL,
`hari` varchar(6) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `tbhari` (`kd_hari`, `hari`) VALUES
('a_Mon', 'Senin'),
('b_Tue', 'Selasa'),
('c_Wed', 'Rabu'),
('d_Thu', 'Kamis'),
('e_Fri', 'Jumat');

我在下面使用此选择查询。

select a.kd_hari,hari,
(select count(kd_hari) from 4a where kd_hari=a.kd_hari)jml
from 4a a,tbhari h where a.kd_hari=h.kd_hari and kd_ruang in('L','P')
union all
select b.kd_hari,hari,
(select count(kd_hari) from 4b where kd_hari=b.kd_hari)jml
from 4b b,tbhari h where b.kd_hari=h.kd_hari and kd_ruang in('L','P')
order by kd_hari;

以上选择查询给我结果

+---------+--------+------+
| kd_hari | hari   | jml  |
+---------+--------+------+
| a_Mon   | Senin  |    4 |
| a_Mon   | Senin  |    4 |
| b_Tue   | Selasa |    4 |
| c_Wed   | Rabu   |    4 |
| c_Wed   | Rabu   |    4 |
| c_Wed   | Rabu   |    4 |
| e_Fri   | Jumat  |    4 |
+---------+--------+------+

问题出在上表结果中,jml列不计数取决于kd_hari列数据

然后,我需要如下的结果。

+---------+--------+------+
| kd_hari | hari   | jml  |
+---------+--------+------+
| a_Mon   | Senin  |    2 |
| a_Mon   | Senin  |    2 |
| b_Tue   | Selasa |    1 |
| c_Wed   | Rabu   |    3 |
| c_Wed   | Rabu   |    3 |
| c_Wed   | Rabu   |    3 |
| e_Fri   | Jumat  |    1 |
+---------+--------+------+

看看jml列有计数kd_hari或hari数据依赖于kd_hari或hari列数据。

非常感谢你的回答。

1 个答案:

答案 0 :(得分:1)

您的查询过于复杂,只需使用group by按组获取计数。更新:由于您在4a和4b表中都有相同的键,并且您希望在一个输出中对它们进行汇总,因此根据您使用的子查询,您需要总计或计数

select kd_hari, hari, sum(rowcount) as rowcount from
    (select a.kd_hari,hari, count(a.kd_hari) as rowcount
    from 4a a,tbhari h where a.kd_hari=h.kd_hari and kd_ruang in('L','P')
    group by a.kd_hari,hari
    union all
    select b.kd_hari,hari, count(b.kd_hari) 
    from 4b a,tbhari h where a.kd_hari=h.kd_hari and kd_ruang in('L','P')
    group by a.kd_hari,hari) as t
group by kd_hari,hari

或者,将4a和4b与一个子查询中的union all组合在一起并将其连接到tbhari表上,并且可以在子查询上使用count()和group by,因为子查询将包含来自两个表的al行。

但是,我编写示例代码的方式可以在加入表时利用现有索引。如果先将它们与union组合,然后再进行连接,则不会使用任何索引进行连接。