我有这个MySQL表:
+------------+-------+---------------+
| date | event | name |
+------------+-------+---------------+
| 2014-08-22 | 3 | A |
| 2014-08-22 | 3 | B |
| 2014-08-26 | 11 | A |
| 2014-08-26 | 15 | B |
| 2014-08-27 | 1 | A |
| 2014-08-27 | 1 | B |
| 2014-09-10 | 2 | A |
| 2014-09-10 | 2 | B |
+------------+-------+---------------+
我想显示如下所示的上述数据:
+------------+-------+---------------+
| date | A | B |
+------------+-------+---------------+
| 2014-08-22 | 3 | 3 |
| 2014-08-26 | 11 | 15 |
| 2014-08-27 | 1 | 1 |
| 2014-09-10 | 2 | 2 |
+------------+-------+---------------+
请帮助我如何获得格式
答案 0 :(得分:0)
好的,我创建了你的表并将其称为tester
:
mysql> select * from tester;
+------------+-------+------+
| date | event | name |
+------------+-------+------+
| 2014-08-22 | 3 | A |
| 2014-08-22 | 3 | B |
| 2014-08-26 | 11 | A |
| 2014-08-26 | 15 | B |
| 2014-08-27 | 1 | A |
| 2014-08-27 | 1 | B |
| 2014-09-10 | 2 | A |
| 2014-09-10 | 2 | B |
+------------+-------+------+
8 rows in set (0.00 sec)
我的想法很简单:
以下是他们演示的完整步骤:
mysql> create view theAs as (select date, event AS A from tester where name='A');
Query OK, 0 rows affected (0.30 sec)
mysql> select * from theAs;
+------------+------+
| date | A |
+------------+------+
| 2014-08-22 | 3 |
| 2014-08-26 | 11 |
| 2014-08-27 | 1 |
| 2014-09-10 | 2 |
+------------+------+
4 rows in set (0.00 sec)
mysql> create view theBs as (select date, event AS B from tester where name='B');
Query OK, 0 rows affected (0.30 sec)
mysql> select * from theBs;
+------------+------+
| date | B |
+------------+------+
| 2014-08-22 | 3 |
| 2014-08-26 | 15 |
| 2014-08-27 | 1 |
| 2014-09-10 | 2 |
+------------+------+
4 rows in set (0.00 sec)
mysql> select theAs.date, theAs.A, theBs.B from theAs join theBs on theAs.date=theBs.date;
+------------+------+------+
| date | A | B |
+------------+------+------+
| 2014-08-22 | 3 | 3 |
| 2014-08-26 | 11 | 15 |
| 2014-08-27 | 1 | 1 |
| 2014-09-10 | 2 | 2 |
+------------+------+------+
4 rows in set (0.00 sec)
Begueradj