MySQL UNION - 将返回的值组合成一组行

时间:2012-05-14 16:55:06

标签: mysql union

我是MySQL的新手,需要请求您的帮助。 我正在使用不同的WHERE子句值来区分联合的2个部分中的同一个表中的2组计数值;像这样:

SELECT Count(column1) as 'RED', null as 'BLUE'
FROM TableA
Where column1 Like 'RED'
UNION
SELECT NULL, Count(column1)
FROM TableA
Where column1 Like 'BLUE'

我希望得到:

RED      BLUE
---      ----
23       55

但实际上我得到的是:

RED      BLUE
---      ----
23       NULL
NULL     55

任何人都可以告诉我我做错了什么以及如何得到我想要的答案? 谢谢你的帮助...

4 个答案:

答案 0 :(得分:3)

这不是UNION的好用例。您可以使用SUMCASE完成您要执行的操作。以下是使用您的查询的示例:

SELECT SUM(CASE WHEN column1 LIKE 'RED' THEN 1 ELSE 0 END) AS red_count,
SUM(CASE WHEN column1 LIKE 'BLUE' THEN 1 ELSE 0 END) AS blue_count
FROM TableA
WHERE column1 LIKE 'RED'
OR column1 LIKE 'BLUE'

答案 1 :(得分:2)

以下是您需要的查询

select
    A.colorcount RED,
    B.colorcount BLUE
from
    (select count(1) colorcount from tableA where column1='RED') A,
    (select count(1) colorcount from tableA where column1='BLUE') B
;

以下是样本数据

mysql> drop database if exists encue;
Query OK, 1 row affected (0.05 sec)

mysql> create database encue;
Query OK, 1 row affected (0.01 sec)

mysql> use encue
Database changed
mysql> create table tableA
    -> (
    ->     id int not null auto_increment,
    ->     column1 varchar(20),
    ->     primary key (id),
    ->     key (column1)
    -> );
Query OK, 0 rows affected (0.13 sec)

mysql> insert into tableA (column1) values
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('RED'),('BLUE'),('BLUE'),('BLUE'),('RED'),('BLUE'),
    -> ('BLUE'),('BLUE'),('BLUE'),('BLUE'),('BLUE'),('BLUE'),
    -> ('BLUE'),('BLUE'),('BLUE'),('BLUE'),('BLUE'),('RED');
select count(1) colorcount,column1 from tableA group by column1;
Query OK, 78 rows affected (0.10 sec)
Records: 78  Duplicates: 0  Warnings: 0

mysql> select count(1) colorcount,column1 from tableA group by column1;
+------------+---------+
| colorcount | column1 |
+------------+---------+
|         55 | BLUE    |
|         23 | RED     |
+------------+---------+
2 rows in set (0.00 sec)

以下是您要查找的输出:

mysql> select A.colorcount RED,B.colorcount BLUE from
    -> (select count(1) colorcount from tableA where column1='RED') A,
    -> (select count(1) colorcount from tableA where column1='BLUE') B
    -> ;
+-----+------+
| RED | BLUE |
+-----+------+
|  23 |   55 |
+-----+------+
1 row in set (0.00 sec)

mysql>

试一试!!!

答案 2 :(得分:0)

你有没有尝试过:

SELECT Count(column1) as red, null as blue
FROM TableA Where column1 Like 'RED'
UNION
SELECT NULL as red, Count(column1) as blue
FROM TableA Where column1 Like 'BLUE' 

答案 3 :(得分:0)

尝试此SQL语句

SELECT Count(A.column1) as red, Count(B.column1) as blue FROM TableA as A,  TableA as B Where A.column1 Like 'RED' AND B.column1 Like 'BLUE'