这是您需要的查询
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>
试试看 !!!