mysql的使用15

mysql> #滑动订单问题
mysql> create table MonthlyOrders
    -> (
    -> ordermonth date primary key
    -> ordernum int unsigned
    -> );
ERROR 1046 (3D000): No database selected
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> create table MonthlyOrders
    -> (
    -> ordermonth date primary key
    -> ordernum int unsigned
    -> );
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 'ordernum int unsigned
)' at line 4
mysql> create table MonthlyOrders
    -> (
    -> ordermonth date primary key
    -> ordernum int
    -> );
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 'ordernum int
)' at line 4
mysql> create table MonthlyOrders
    -> (
    -> ordermonth date primary key,
    -> ordernum int unsigned
    -> );
Query OK, 0 rows affected (0.09 sec)

mysql> #奶奶的  都好没打
mysql>
mysql> insert into MonthlyOrders select '2010-02-01',23;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2010-03-01',26;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2010-04-01',24;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2010-05-01',27;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2010-06-01',26;
Query OK, 1 row affected (0.05 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2010-07-01',32;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2010-08-01',34;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2010-09-01',30;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

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

mysql> insert into MonthlyOrders select '2010-11-01',32;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2010-12-01',33;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2011-01-01',31;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

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

mysql> insert into MonthlyOrders select '2011-03-01',34;
Query OK, 1 row affected (0.05 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2011-04-01',38;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2011-05-01',39;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2011-06-01',35;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2011-07-01',49;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2011-08-01',56;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2011-09-01',55;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into MonthlyOrders select '2011-10-01',74;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

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

mysql> insert into MonthlyOrders select '2011-12-01',14;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> 色了才同
    -> ;
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 '色了才同' at line 1
mysql> select * from MonthlyOrders;
+------------+----------+
| ordermonth | ordernum |
+------------+----------+
| 2010-02-01 |       23 |
| 2010-03-01 |       26 |
| 2010-04-01 |       24 |
| 2010-05-01 |       27 |
| 2010-06-01 |       26 |
| 2010-07-01 |       32 |
| 2010-08-01 |       34 |
| 2010-09-01 |       30 |
| 2010-10-01 |       31 |
| 2010-11-01 |       32 |
| 2010-12-01 |       33 |
| 2011-01-01 |       31 |
| 2011-02-01 |       34 |
| 2011-03-01 |       34 |
| 2011-04-01 |       38 |
| 2011-05-01 |       39 |
| 2011-06-01 |       35 |
| 2011-07-01 |       49 |
| 2011-08-01 |       56 |
| 2011-09-01 |       55 |
| 2011-10-01 |       74 |
| 2011-11-01 |       75 |
| 2011-12-01 |       14 |
+------------+----------+
23 rows in set (0.00 sec)

mysql> #滑动订单问题是指为每个月返回上一年度(季度或月度)的华东订单数,即为每个月份N ,返回从月份N-11到月份N的订单总数
mysql>
mysql> select date_format(a.ordermonth, '%Y%m') as frommonth,
    -> date_format(b.ordermonth, '%Y%m') as tomonth,
    -> sum(c.ordernum) as orders
    -> from MonthlyOrders a
    -> inner join MonthlyOrders b
    -> on date_add(a.ordermonth, interval 11 month)=b.ordermonth
    -> inner join MonthlyOrders c
    -> on c.ordermonth between a.ordermonth and b.ordermonth
    -> group by a.ordermonth, b.ordermonth;
+-----------+---------+--------+
| frommonth | tomonth | orders |
+-----------+---------+--------+
| 201002    | 201101  |    349 |
| 201003    | 201102  |    360 |
| 201004    | 201103  |    368 |
| 201005    | 201104  |    382 |
| 201006    | 201105  |    394 |
| 201007    | 201106  |    403 |
| 201008    | 201107  |    420 |
| 201009    | 201108  |    442 |
| 201010    | 201109  |    467 |
| 201011    | 201110  |    510 |
| 201012    | 201111  |    553 |
| 201101    | 201112  |    534 |
+-----------+---------+--------+
12 rows in set (0.00 sec)

mysql>
mysql> #基于上种情况 我们还可以找出季度的情况
mysql>
mysql> select date_format(a.ordermonth, '%Y%m') as frommonth, date_format(b.ordermonth, '%Y%m') as tomonth, sum(c.ordernum) as orders from MonthlyOrders a inner join MonthlyOrders b on date_add(a.ordermonth, interval 2 month)=b.ordermonth and month(a.ordermonth)%3=1 inner join MonthlyOrders c on c.ordermonth between a.ordermonth and b.ordermonth group by a.ordermonth, b.ordermonth;
+-----------+---------+--------+
| frommonth | tomonth | orders |
+-----------+---------+--------+
| 201004    | 201006  |     77 |
| 201007    | 201009  |     96 |
| 201010    | 201012  |     96 |
| 201101    | 201103  |     99 |
| 201104    | 201106  |    112 |
| 201107    | 201109  |    160 |
| 201110    | 201112  |    163 |
+-----------+---------+--------+
7 rows in set (0.01 sec)

mysql> #连接算法
mysql> #Nested-Loops Join and Classic Hash Join and then future will be support hash join
mysql>
mysql> #首先介绍Simple Nested-Loops Join
mysql> #for each row r in R do
mysql> # for each row s in S do
mysql> #   if r and s satisfy the join condition
mysql> #        then output the tuple<r,s>
mysql> #其中R为外部表  S为内部表      算法效率O(Rn + Rn * Sn)
mysql> #算法这里就不深入了  比较晦涩难懂  嘿嘿  自己想看的可以看看
mysql>
mysql> #集合操作    一般是将这些操作视为垂直操作
mysql> #MYSQL数据库支持两种集合操作  union all  and union distinct
mysql>
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              |
| yeartest       |
| z              |
+----------------+
24 rows in set (0.00 sec)

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

mysql> drop table y;
ERROR 1051 (42S02): Unknown table 'y'
mysql> create table x (a, char(1)) engine = innodb;
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 ' char(1)) engine = innodb' at line 1
mysql> create table x (a, char(1)) engine=innodb;
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 ' char(1)) engine=innodb' at line 1
mysql> create table x (a, char(1))engine=innodb;
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 ' char(1))engine=innodb' at line 1
mysql>
mysql>
mysql> create table y(a char(1))engine=innodb;
Query OK, 0 rows affected (0.10 sec)

mysql> create table x(a char(1))engine=innodb;
Query OK, 0 rows affected (0.09 sec)

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

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

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

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

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

mysql> select a as m from x
    -> union
    -> select a as n from y;
+------+
| m    |
+------+
| a    |
| b    |
| c    |
+------+
3 rows in set (0.00 sec)

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

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

mysql> select a as m from x union select a as n from y;
+------+
| m    |
+------+
| a    |
| b    |
| c    |
| w    |
| q    |
+------+
5 rows in set (0.00 sec)

mysql> #select查询的不同  
mysql> #只有最后一个select空调一应用into outfile,但是整个集合的操作将被输出到文件中
mysql> #不能在select语句中使用high_priority关键字
mysql>
mysql> select a from x union select a into outfile '/home/MySQL/record.txt' from y;
ERROR 1 (HY000): Can't create/write to file '/home/MySQL/record.txt' (Errcode: 13)
mysql> select a from x union select a into outfile '/home/MySQL/record.txt' from y;
ERROR 1086 (HY000): File '/home/MySQL/record.txt' already exists
mysql> select a from x union select a into outfile '/home/MySQL/record.txt' from y;
ERROR 1 (HY000): Can't create/write to file '/home/MySQL/record.txt' (Errcode: 13)
mysql> #有点问题。。。。。
mysql>
mysql>
mysql> #如果导入成功  可以使用下面的命令查看  system cat path
mysql>
mysql> (select a from x limit 2) union (select a from y);
+------+
| a    |
+------+
| a    |
| b    |
| w    |
| q    |
+------+
4 rows in set (0.00 sec)

mysql> exit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值