mysql的使用18

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> #pivoting
mysql> #pivoting 是一项可以把行旋转为列的技术   在执行pivoting的过程中可能会使用到聚合
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| MonthlyOrders  |
| Nums           |
| a              |
| animals        |
| charTest       |
| customers      |
| dept_manager   |
| emp            |
| employees      |
| g              |
| mintable       |
| new_emp        |
| orders         |
| sales          |
| sessions       |
| t              |
| test01         |
| timetest       |
| tt             |
| ttt            |
| updatetime     |
| x              |
| xx             |
| y              |
| yeartest       |
| yy             |
| z              |
+----------------+
27 rows in set (0.01 sec)

mysql> drop table t;
Query OK, 0 rows affected (0.06 sec)

mysql> create table t(
    -> id int,
    -> attribute varchar(10),
    -> value varchar(20),
    -> primary key(id, attribute));
Query OK, 0 rows affected (0.08 sec)

mysql> insert into t select 1,'attr1','BMW';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 1,'attr2','100';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 1,'attr3','2010-01-01';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 2,'attr2','200';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 2,'attr3','2010-03-04';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 2,'attr4','M';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 2,'attr5','55.60';
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 3,'attr1','SUV';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 3,'attr2','10';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 3,'attr3','2011-11-11';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t;
+----+-----------+------------+
| id | attribute | value      |
+----+-----------+------------+
|  1 | attr1     | BMW        |
|  1 | attr2     | 100        |
|  1 | attr3     | 2010-01-01 |
|  2 | attr2     | 200        |
|  2 | attr3     | 2010-03-04 |
|  2 | attr4     | M          |
|  2 | attr5     | 55.60      |
|  3 | attr1     | SUV        |
|  3 | attr2     | 10         |
|  3 | attr3     | 2011-11-11 |
+----+-----------+------------+
10 rows in set (0.01 sec)

mysql>
mysql> select id,
    -> max(case when attribute='attr1' then value end) as attr1,
    -> max(case when attribute='attr2' then value end) as attr2,
    -> max(case when attribute='attr3' then value end) as attr3,
    -> max(case when attribute='attr4' then value end) as attr4,
    -> max(case when attribute='attr5' then value end) as attr5,
    -> from t
    -> group by id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from t
group by id' at line 7
mysql> select id, max(case when attribute='attr1' then value end) as attr1, max(case when attribute='attr2' then value end) as attr2, max(case when attribute='attr3' then value end) as attr3, max(case when attribute='attr4' then value end) as attr4, max(case when attribute='attr5' then value end) as attr5  from t group by id;
+----+-------+-------+------------+-------+-------+
| id | attr1 | attr2 | attr3      | attr4 | attr5 |
+----+-------+-------+------------+-------+-------+
|  1 | BMW   | 100   | 2010-01-01 | NULL  | NULL  |
|  2 | NULL  | 200   | 2010-03-04 | M     | 55.60 |
|  3 | SUV   | 10    | 2011-11-11 | NULL  | NULL  |
+----+-------+-------+------------+-------+-------+
3 rows in set (0.00 sec)

mysql> #注意最后一个max不要加逗号
mysql> #pivoting先根据id进行分组,确定行列互转后记录的行数,之后通过已知的五个属性来确定互转后有5列数据,并通过case得到每列的值,由于使用了分组函数取得列的值,这里使用max函数
mysql>
mysql>
mysql> #关系除法    和常见的关系运算join,semi join一样,都是一个关系代数
mysql>
mysql> drop table t;
Query OK, 0 rows affected (0.04 sec)

mysql> create table t(
    -> orderid varchar(10) not null,
    -> productid int not null,
    -> primary key(orderid, productid));
Query OK, 0 rows affected (0.10 sec)

mysql> insert into t select 'A',1;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 'A',2;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 'A',3;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 'A',4;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> insert into t select 'B',2;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 'B',3;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 'B',4;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> insert into t select 'C',3;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into t select 'C',4;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>
mysql> insert into t select 'D',4;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t;
+---------+-----------+
| orderid | productid |
+---------+-----------+
| A       |         1 |
| A       |         2 |
| A       |         3 |
| A       |         4 |
| B       |         2 |
| B       |         3 |
| B       |         4 |
| C       |         3 |
| C       |         4 |
| D       |         4 |
+---------+-----------+
10 rows in set (0.00 sec)

mysql> select orderid from (
    -> select orderid,
    -> max(case when productid=2 then 1 end) as p2,
    -> max(case when productid=3 then 1 end) as p3,
    -> max(case when productid=4 then 1 end) as p4
    -> from t group by orderid) as P
    -> where p2=1 and p3=1 and p4=1;
+---------+
| orderid |
+---------+
| A       |
| B       |
+---------+
2 rows in set (0.00 sec)

mysql> #以上查询的是包含2,3,4订单号的产品
mysql>
mysql> #其实 这里将max改为count似乎更合理  因为没有的需要用0来 而不是null
mysql>
mysql> create table tt(
    -> orderid int not null,
    -> orderdate date not null,
    -> empid int not null,
    -> custid varchar(10) not null,
    -> qty int not null,
    -> primary key(orderid, orderdate));
ERROR 1050 (42S01): Table 'tt' already exists
mysql> create table tttt( orderid int not null, orderdate date not null, empid int not null, custid varchar(10) not null, qty int not null, primary key(orderid, orderdate));
Query OK, 0 rows affected (0.10 sec)

mysql> desc tttt;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| orderid   | int(11)     | NO   | PRI | NULL    |       |
| orderdate | date        | NO   | PRI | NULL    |       |
| empid     | int(11)     | NO   |     | NULL    |       |
| custid    | varchar(10) | NO   |     | NULL    |       |
| qty       | int(11)     | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> insert into tttt select 1,'2010-01-02',3,'A',10;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tttt select 2,'2010-04-02',2,'B',20;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tttt select 3,'2010-05-02',1,'A',30;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tttt select 4,'2010-07-02',3,'D',40;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tttt select 5,'2011-01-02',4,'A',20;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tttt select 6,'2011-01-02',3,'B',30;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tttt select 7,'2011-01-02',1,'C',40;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tttt select 8,'2009-01-02',2,'A',10;
Query OK, 1 row affected (0.05 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tttt select 9,'2009-01-02',3,'B',20;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from tttt;
+---------+------------+-------+--------+-----+
| orderid | orderdate  | empid | custid | qty |
+---------+------------+-------+--------+-----+
|       1 | 2010-01-02 |     3 | A      |  10 |
|       2 | 2010-04-02 |     2 | B      |  20 |
|       3 | 2010-05-02 |     1 | A      |  30 |
|       4 | 2010-07-02 |     3 | D      |  40 |
|       5 | 2011-01-02 |     4 | A      |  20 |
|       6 | 2011-01-02 |     3 | B      |  30 |
|       7 | 2011-01-02 |     1 | C      |  40 |
|       8 | 2009-01-02 |     2 | A      |  10 |
|       9 | 2009-01-02 |     3 | B      |  20 |
+---------+------------+-------+--------+-----+
9 rows in set (0.00 sec)

mysql> select cusid,year(orderdate) as year, sum(qty) as sum_qty
    -> from t group by custid,year(orderdate);
ERROR 1054 (42S22): Unknown column 'cusid' in 'field list'
mysql>
mysql> select custid,year(orderdate) as year, sum(qty) as sum_qty from t group by custid,year(orderdate);
ERROR 1054 (42S22): Unknown column 'custid' in 'field list'
mysql>
mysql> select custid,year(orderdate) as year, sum(qty) as sum_qty from tttt group by custid,year(orderdate);
+--------+------+---------+
| custid | year | sum_qty |
+--------+------+---------+
| A      | 2009 |      10 |
| A      | 2010 |      40 |
| A      | 2011 |      20 |
| B      | 2009 |      20 |
| B      | 2010 |      20 |
| B      | 2011 |      30 |
| C      | 2011 |      40 |
| D      | 2010 |      40 |
+--------+------+---------+
8 rows in set (0.00 sec)

mysql> #又是tttt惹得祸。。。。。
mysql> #下面使用旋转将数据变得更加清晰
mysql>
mysql> select custid,
    -> ifnull(sum(case when orderyear=2009 then qty end),0) as '2009',
    -> ifnull(sum(case when orderyear=2010 then qty end),0) as '2010',
    -> ifnull(sum(case when orderyear=2011 then qty end),0) as '2011'
    -> from
    -> (select custid,year(orderdate) as orderyrear, qty from t) as p
    -> group by custid;
ERROR 1054 (42S22): Unknown column 'custid' in 'field list'
mysql>
mysql> select custid, ifnull(sum(case when orderyear=2009 then qty end),0) as '2009', ifnull(sum(case when orderyear=2010 then qty end),0) as '2010', ifnull(sum(case when orderyear=2011 then qty end),0) as '2011' from  (select custid,year(orderdate) as orderyrear, qty from tttt) as p group by custid;
ERROR 1054 (42S22): Unknown column 'orderyear' in 'field list'
mysql>
mysql> select custid, ifnull(sum(case when orderyear=2009 then qty end),0) as '2009', ifnull(sum(case when orderyear=2010 then qty end),0) as '2010', ifnull(sum(case when orderyear=2011 then qty end),0) as '2011' from  (select custid,year(orderdate) as orderyear, qty from tttt) as p group by custid;
+--------+------+------+------+
| custid | 2009 | 2010 | 2011 |
+--------+------+------+------+
| A      |   10 |   40 |   20 |
| B      |   20 |   20 |   30 |
| C      |    0 |    0 |   40 |
| D      |    0 |   40 |    0 |
+--------+------+------+------+
4 rows in set (0.00 sec)

mysql> #ok  这样就非常清晰了
mysql>
mysql> #unpivoting即为pivoting的反响操作 即将列旋转为行。。。
mysql> drop table pp;
ERROR 1051 (42S02): Unknown table 'pp'
mysql> create table pp(
    -> custid varchar(10) not null,
    -> y2009 int null,
    -> y2010 int null,
    -> y2011 int null,
    -> primary key(custid));
Query OK, 0 rows affected (0.09 sec)

mysql> #现在我们可以将上面的表 再次进行行列互转
mysql>
mysql> insert into p
    -> select
    -> custid,
    -> ifnull(sum(case when orderyear=2009 then qty end),0) as '2009',
    -> ifnull(sum(case when orderyear=2010 then qty end),0) as '2010',
    -> ifnull(sum(case when orderyear=2011 then qty end),0) as '2011'
    -> from (select custid, year(orderdate) as orderyear, qty from t) as P
    -> group by custid;
ERROR 1146 (42S02): Table 'test.p' doesn't exist
mysql>
mysql> insert into p select  custid, ifnull(sum(case when orderyear=2009 then qty end),0) as '2009', ifnull(sum(case when orderyear=2010 then qty end),0) as '2010', ifnull(sum(case when orderyear=2011 then qty end),0) as '2011' from (select custid, year(orderdate) as orderyear, qty from t) as pp group by custid;
ERROR 1146 (42S02): Table 'test.p' doesn't exist
mysql>
mysql>
mysql> insert into pp select  custid, ifnull(sum(case when orderyear=2009 then qty end),0) as '2009', ifnull(sum(case when orderyear=2010 then qty end),0) as '2010', ifnull(sum(case when orderyear=2011 then qty end),0) as '2011' from (select custid, year(orderdate) as orderyear, qty from t) as pp group by custid;
ERROR 1054 (42S22): Unknown column 'custid' in 'field list'
mysql>
mysql> desc pp;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| custid | varchar(10) | NO   | PRI | NULL    |       |
| y2009  | int(11)     | YES  |     | NULL    |       |
| y2010  | int(11)     | YES  |     | NULL    |       |
| y2011  | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> insert into pp select  custid, ifnull(sum(case when orderyear=2009 then qty end),0) as '2009', ifnull(sum(case when orderyear=2010 then qty end),0) as '2010', ifnull(sum(case when orderyear=2011 then qty end),0) as '2011' from (select custid, year(orderdate) as orderyear, qty from tttt) as pp group by custid;
Query OK, 4 rows affected (0.04 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> #ok
mysql> select * from pp;
+--------+-------+-------+-------+
| custid | y2009 | y2010 | y2011 |
+--------+-------+-------+-------+
| A      |    10 |    40 |    20 |
| B      |    20 |    20 |    30 |
| C      |     0 |     0 |    40 |
| D      |     0 |    40 |     0 |
+--------+-------+-------+-------+
4 rows in set (0.00 sec)

mysql> #现在unpoviting
mysql> select custid, orderyear,
    -> case orderyear
    ->  when 2009 then y2009
    ->  when 2010 then y2010
    ->  when 2011 then y2011
    -> end as qty
    -> from pp,
    -> (select 2009 as orderyear union all select 2010 union all select 2011) as o;
+--------+-----------+------+
| custid | orderyear | qty  |
+--------+-----------+------+
| A      |      2009 |   10 |
| A      |      2010 |   40 |
| A      |      2011 |   20 |
| B      |      2009 |   20 |
| B      |      2010 |   20 |
| B      |      2011 |   30 |
| C      |      2009 |    0 |
| C      |      2010 |    0 |
| C      |      2011 |   40 |
| D      |      2009 |    0 |
| D      |      2010 |   40 |
| D      |      2011 |    0 |
+--------+-----------+------+
12 rows in set (0.00 sec)

mysql> #现在发现还有0   再次进行过滤
mysql> select custid,orderyear,qty from ( select custid, orderyear, case orderyear  when 2009 then y2009  when 2010 then y2010  when 2011 then y2011 end as qty from pp, (select 2009 as orderyear union all select 2010 union all select 2011) as o) as m where qty<>0;
+--------+-----------+------+
| custid | orderyear | qty  |
+--------+-----------+------+
| A      |      2009 |   10 |
| A      |      2010 |   40 |
| A      |      2011 |   20 |
| B      |      2009 |   20 |
| B      |      2010 |   20 |
| B      |      2011 |   30 |
| C      |      2011 |   40 |
| D      |      2010 |   40 |
+--------+-----------+------+
8 rows in set (0.00 sec)

mysql> #ok  搞定
mysql>
mysql> exit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值