mysql学习笔记

来源:互联网 发布:深圳淘宝免费运营 编辑:程序博客网 时间:2024/06/03 22:41

前几天简单的学习了一下mysql,在命令执行的时候保存了text,都是很简单的测试,这里罗列一下,以备查看。


  • 链接数据库
C:\Users\Administrator>mysql -uroot -p123456
  • 查看所有库
mysql> show databases;

+——————–+
| Database |
+——————–+
| information_schema |
| bms |
| books |
| db_chepiao |
| db_fuzhuang |
| db_hotelmaster |
| db_personmanage |
| db_storage |
| hainangl_db |
| hngl |
| jspjiazheng |
| lk |
| music |
| mysql |
| mytest1 |
| personal |
| shyj |
| shyjbf |
| test |
| whgl |
+——————–+

  • 使用库
mysql> use mytest1;
  • 显示所有表格
mysql> show tables;

+——————-+
| Tables_in_mytest1 |
+——————-+
| test |
| hello |
+——————-+

  • 将操作的sql保存到一个文件中
mysql> tee K:/0825.sql
  • 查询
mysql> select * from newstu;

+——+——-+
| snum | sname |
+——+——-+
| 1 | 张三 |
| 2 | 李四 |
| 2 | 李四 |
+——+——-+

  • 多条插入
mysql> insert into newstu values (3,'王五'),(4,'赵四');
mysql> select * from newstu;

+——+——-+
| snum | sname |
+——+——-+
| 1 | 张三 |
| 2 | 李四 |
| 2 | 李四 |
| 3 | 王五 |
| 4 | 赵四 |
+——+——-+

  • 更新
mysql> update newstu set sname='李四2' where snum=2;mysql> select * from newstu;

+——+——-+
| snum | sname |
+——+——-+
| 1 | 张三 |
| 2 | 李四2 |
| 2 | 李四2 |
| 3 | 王五 |
| 4 | 赵四 |
+——+——-+

  • 删除
mysql> delete from newstu where snum=2 and sname='李四2';mysql> select * from newstu;

+——+——-+
| snum | sname |
+——+——-+
| 1 | 张三 |
| 3 | 王五 |
| 4 | 赵四 |
+——+——-+

  • 创建表格
mysql> create table class(    -> sname varchar(20) not null default '',    -> age tinyint not null default 0);
  • 显示表格信息
mysql> desc class;

+——-+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————-+——+—–+———+——-+
| sname | varchar(20) | NO | | | |
| age | tinyint(4) | NO | | 0 | |
+——-+————-+——+—–+———+——-+

  • 测试tinyint的限度 -128~127
mysql> insert into class values ('zhang',255);mysql> select * from class;

+——-+—–+
| sname | age |
+——-+—–+
| zhang | 127 |
+——-+—–+

mysql> insert into class values('li',-128);mysql> select * from class;

+——-+——+
| sname | age |
+——-+——+
| zhang | 127 |
| li | -128 |
+——-+——+

  • unsigned 限制字段为正数
mysql> alter table class add score tinyint unsigned not null default 0;mysql> desc class;

+——-+———————+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+———————+——+—–+———+——-+
| sname | varchar(20) | NO | | | |
| age | tinyint(4) | NO | | 0 | |
| score | tinyint(3) unsigned | NO | | 0 | |
+——-+———————+——+—–+———+——-+

(1)正数插入正常

mysql> insert into class values ('wang',25,255);mysql> select * from class;

+——-+——+——-+
| sname | age | score |
+——-+——+——-+
| zhang | 127 | 0 |
| li | -128 | 0 |
| wang | 25 | 255 |
+——-+——+——-+

(2)负数插入后被转为了0

mysql> insert into class values ('zhao',-129,-1);mysql> insert into class values ('zhao',-129,-1);mysql> select * from class;

+——-+——+——-+
| sname | age | score |
+——-+——+——-+
| zhang | 127 | 0 |
| li | -128 | 0 |
| wang | 25 | 255 |
| zhao | -128 | 0 |

  • zerofill 0填充
mysql> alter table class add snum smallint(7) zerofill not null default 0;mysql> desc class;

+——-+——————————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+——————————-+——+—–+———+——-+
| sname | varchar(20) | NO | | | |
| age | tinyint(4) | NO | | 0 | |
| score | tinyint(3) unsigned | NO | | 0 | |
| snum | smallint(7) unsigned zerofill | NO | | 0000000 | |
+——-+——————————-+——+—–+———+——-+

mysql> select * from class;

+——-+——+——-+———+
| sname | age | score | snum |
+——-+——+——-+———+
| zhang | 127 | 0 | 0000000 |
| li | -128 | 0 | 0000000 |
| wang | 25 | 255 | 0000000 |
| zhao | -128 | 0 | 0000000 |
+——-+——+——-+———+

(1)插入两条记录测试

mysql> insert into class (sname) values ('qian');mysql> select * from class;

+——-+——+——-+———+
| sname | age | score | snum |
+——-+——+——-+———+
| zhang | 127 | 0 | 0000000 |
| li | -128 | 0 | 0000000 |
| wang | 25 | 255 | 0000000 |
| zhao | -128 | 0 | 0000000 |
| qian | 0 | 0 | 0000000 |
+——-+——+——-+———+

mysql> insert into class(sname,snum) values('li',15);mysql> select * from class;

+——-+——+——-+———+
| sname | age | score | snum |
+——-+——+——-+———+
| zhang | 127 | 0 | 0000000 |
| li | -128 | 0 | 0000000 |
| wang | 25 | 255 | 0000000 |
| zhao | -128 | 0 | 0000000 |
| qian | 0 | 0 | 0000000 |
| li | 0 | 0 | 0000015 |
+——-+——+——-+———+

  • float和decimal类型

(1)创建一个表格

mysql> create table salary(    -> sname varchar(20) not null default '',    -> gongzi float(6,2) unsigned not null default 0,    -> bonus float(5,2) unsigned not null default 0);mysql> desc salary;

+——–+———————+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——–+———————+——+—–+———+——-+
| sname | varchar(20) | NO | | | |
| gongzi | float(6,2) unsigned | NO | | 0.00 | |
| bonus | float(5,2) unsigned | NO | | 0.00 | |

(2)插入两条记录

mysql> insert into salary (sname) values ('zhangs');Query OK, 1 row affected (0.02 sec)mysql> insert into salary values ('li',9999.99,999.99);Query OK, 1 row affected (0.01 sec)mysql> select * from salary;

+——–+———+——–+
| sname | gongzi | bonus |
+——–+———+——–+
| zhangs | 0.00 | 0.00 |
| li | 9999.99 | 999.99 |

(3)再创建一个表格

mysql> create table bank (    -> id int not null default 0,    -> acc1 float(9,2) unsigned not null default 0.00,    -> acc2 decimal(9,2) unsigned not null default 0.00);mysql> desc bank;

+——-+———————–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+———————–+——+—–+———+——-+
| id | int(11) | NO | | 0 | |
| acc1 | float(9,2) unsigned | NO | | 0.00 | |
| acc2 | decimal(9,2) unsigned | NO | | 0.00 | |
+——-+———————–+——+—–+———+——-+

(4)分别为float和decimal插入数据,可看到float有精度丢失,而decimal正常

mysql> insert into bank values(1,1234567.23,1234567.23);Query OK, 1 row affected (0.01 sec)mysql> select * from bank;

+—-+————+————+
| id | acc1 | acc2 |
+—-+————+————+
| 1 | 1234567.25 | 1234567.23 |
+—-+————+————+

  • char类型
mysql> create table strTest(    -> ch char(6) not null default '',    -> vch varchar(6) not null default '');mysql> desc strTest;

+——-+————+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————+——+—–+———+——-+
| ch | char(6) | NO | | | |
| vch | varchar(6) | NO | | | |

mysql> insert into strTest values('001 ','001 ');mysql> select * from strTest;

+—–+——+
| ch | vch |
+—–+——+
| 001 | 001 |
+—–+——+

  • 使用链接函数contract
mysql> select concat(ch,'!') from strTest;

+—————-+
| concat(ch,’!’) |
+—————-+
| 001! |

mysql> select concat(ch,'!') as ch,concat(vch,'!') as vch from strTest;

+——+——-+
| ch | vch |
+——+——-+
| 001! | 001 ! |
+——+——-+

  • text类型,blob类型
mysql> alter table strTest add textType text;mysql> alter table strTest add blobType blob;mysql> desc strTest;

+———-+————+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+———-+————+——+—–+———+——-+
| ch | char(6) | NO | | | |
| vch | varchar(6) | NO | | | |
| textType | text | YES | | NULL | |
| blobType | blob | YES | | NULL | |

  • timestamp类型
mysql> create table mytest3(    -> loginDate timestamp default current_timestamp);mysql> desc mytest3;

+———–+———–+——+—–+——————-+——-+
| Field | Type | Null | Key | Default | Extra |
+———–+———–+——+—–+——————-+——-+
| loginDate | timestamp | YES | | CURRENT_TIMESTAMP | |
+———–+———–+——+—–+——————-+——-+

(1)在最后位置插入字段

mysql> alter table mytest3 add sale decimal(8,2) default 0.00;mysql> desc mytest3;

+———–+————–+——+—–+——————-+——-+
| Field | Type | Null | Key | Default | Extra |
+———–+————–+——+—–+——————-+——-+
| loginDate | timestamp | YES | | CURRENT_TIMESTAMP | |
| sale | decimal(8,2) | YES | | 0.00 | |

+———–+————–+——+—–+——————-+——-+

(2)在某个字段后加入字段

mysql> alter table mytest3 add sex tinyint after loginDate;mysql> desc mytest3;

+———–+————–+——+—–+——————-+——-+
| Field | Type | Null | Key | Default | Extra |
+———–+————–+——+—–+——————-+——-+
| loginDate | timestamp | YES | | CURRENT_TIMESTAMP | |
| sex | tinyint(4) | YES | | NULL | |
| sale | decimal(8,2) | YES | | 0.00 | |
+———–+————–+——+—–+——————-+——-+

  • 修改字段
mysql> alter table mytest3 modify sex tinyint not null default 1;mysql> desc mytest3;

+———–+————–+——+—–+——————-+—————-+
| Field | Type | Null | Key | Default | Extra |
+———–+————–+——+—–+——————-+—————-+
| id | int(11) | NO | PRI | NULL | auto_increment |
| loginDate | timestamp | YES | | CURRENT_TIMESTAMP | |
| sex | tinyint(4) | NO | | 1 | |
| sale | decimal(8,2) | YES | | 0.00 | |
+———–+————–+——+—–+——————-+—————-+

  • 删除字段
mysql> alter table mytest3 drop sex;mysql> desc mytest3;

+———–+————–+——+—–+——————-+—————-+
| Field | Type | Null | Key | Default | Extra |
+———–+————–+——+—–+——————-+—————-+
| id | int(11) | NO | PRI | NULL | auto_increment |
| loginDate | timestamp | YES | | CURRENT_TIMESTAMP | |
| sale | decimal(8,2) | YES | | 0.00 | |

+———–+————–+——+—–+——————-+—————-+

  • 修改字段名
mysql> alter table mytest3 change sale  sales decimal(8,2);mysql> desc mytest3;

+———–+————–+——+—–+——————-+—————-+
| Field | Type | Null | Key | Default | Extra |
+———–+————–+——+—–+——————-+—————-+
| id | int(11) | NO | PRI | NULL | auto_increment |
| loginDate | timestamp | YES | | CURRENT_TIMESTAMP | |
| sales | decimal(8,2) | YES | | NULL | |
+———–+————–+——+—–+——————-+—————-+

  • 数学函数

(1)创建mytest2

mysql> create table mytest2(    -> id int unsigned auto_increment primary key,    -> idnum int(7) unsigned zerofill not null default 0000000,    -> sale decimal(8,2) default 0.00,    -> sex tinyint,    -> loginDate timestamp default CURRENT_TIMESTAMP)    -> engine myisam charset utf8;mysql> desc mytest2;

+———–+————————–+——+—–+——————-+—————-+
| Field | Type | Null | Key | Default | Extra |
+———–+————————–+——+—–+——————-+—————-+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| idnum | int(7) unsigned zerofill | NO | | 0000000 | |
| sale | decimal(8,2) | YES | | 0.00 | |
| sex | tinyint(4) | YES | | NULL | |
| loginDate | timestamp | YES | | CURRENT_TIMESTAMP | |
+———–+————————–+——+—–+——————-+—————-+

(2)插入四条数据

mysql> insert into mytest2(id,idnum,sale,sex) values (1,'01',10.00,0) ,(2,'02',20.00,1),(3,'01',13.00,0),(4,'05',90.00,0);mysql> select * from mytest2;

+—-+———+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+———+——-+——+———————+
| 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
+—-+———+——-+——+———————+

  • sum函数
mysql> select sum(sale) from mytest2;

+———–+
| sum(sale) |
+———–+
| 133.00 |
+———–+

  • avg函数
mysql> select avg(sale) from mytest2;

+———–+
| avg(sale) |
+———–+
| 33.250000 |
+———–+

  • count()函数
mysql> select count(*) from mytest2;

+———-+
| count(*) |
+———-+
| 4 |
+———-+

  • max()函数
mysql> select max(sale) from mytest2;

+———–+
| max(sale) |
+———–+
| 90.00 |
+———–+

  • min函数
mysql> select min(sale) from mytest2;

+———–+
| min(sale) |
+———–+
| 10.00 |
+———–+

  • group by
mysql> select id,sum(sale) from mytest2    -> group by id;

+—-+———–+
| id | sum(sale) |
+—-+———–+
| 1 | 10.00 |
| 2 | 20.00 |
| 3 | 13.00 |
| 4 | 90.00 |
+—-+———–+

mysql> select idnum,sum(sale) from mytest2    -> group by idnum;

+———+———–+
| idnum | sum(sale) |
+———+———–+
| 0000001 | 23.00 |
| 0000002 | 20.00 |
| 0000005 | 90.00 |

mysql> select idnum,sum(sale) as mysum from mytest2    -> group by idnum ;

+———+——-+
| idnum | mysum |
+———+——-+
| 0000001 | 23.00 |
| 0000002 | 20.00 |
| 0000005 | 90.00 |
+———+——-+

  • having
mysql> select idnum,sum(sale) as mysum from mytest2    -> group by idnum having mysum>23;

+———+——-+
| idnum | mysum |
+———+——-+
| 0000005 | 90.00 |
+———+——-+

  • order by
mysql> select idnum,sum(sale) as mysum from mytest2    -> where idnum=0000001 group by idnum having mysum>=23 order by idnum desc;

+———+——-+
| idnum | mysum |
+———+——-+
| 0000001 | 23.00 |
+———+——-+

  • limit
mysql> insert into mytest2 (id ,idnum,sale,sex) values    -> (5,0000001,50.00,0),    -> (6,0000002,70.00,0),    -> (7,0000002,70.00,0),    -> (8,0000002,70.00,0),    -> (9,0000002,70.00,0),    -> (10,0000002,70.00,0);mysql> select * from mytest2;

+—-+———+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+———+——-+——+———————+
| 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
+—-+———+——-+——+———————+

mysql> select id,idnum,sale from mytest2      -> where idnum=0000002 order by id desc limit 0,3    -> ;

+—-+———+——-+
| id | idnum | sale |
+—-+———+——-+
| 10 | 0000002 | 70.00 |
| 9 | 0000002 | 70.00 |
| 8 | 0000002 | 70.00 |
+—-+———+——-+

  • 拷贝一张表
mysql> create table mytest5 select * from mytest2;mysql> select * from mytest5;

+—-+———+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+———+——-+——+———————+
| 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
+—-+———+——-+——+———————+

  • in
mysql> select * from mytest2;

+—-+———+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+———+——-+——+———————+
| 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
+—-+———+——-+——+———————+

mysql> select * from class;

+——-+——+——-+———+———+
| sname | age | score | snum | idnum |
+——-+——+——-+———+———+
| zhang | 127 | 0 | 0000000 | 0000001 |
| li | -128 | 0 | 0000000 | 0000001 |
| wang | 25 | 255 | 0000000 | 0000001 |
| zhao | -128 | 0 | 0000000 | 0000001 |
| qian | 0 | 0 | 0000000 | 0000001 |
| li | 0 | 0 | 0000015 | 0000001 |

+——-+——+——-+———+———+

mysql> select * from mytest2 where mytest2.idnum in (select idnum from class);

+—-+———+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+———+——-+——+———————+
| 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
+—-+———+——-+——+———————+

  • exists
mysql> select * from mytest2 where exists(select * from class where class.idnum=mytest2.idnum);

+—-+———+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+———+——-+——+———————+
| 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
+—-+———+——-+——+———————+

  • 笛卡尔积
mysql> select * from class,mytest2;

+——-+——+——-+———+———+—-+———+——-+——+———————+
| sname | age | score | snum | idnum | id | idnum | sale | sex | loginDate |
+——-+——+——-+———+———+—-+———+——-+——+———————+
| zhang | 127 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| li | -128 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| wang | 25 | 255 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| zhao | -128 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| qian | 0 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| li | 0 | 0 | 0000015 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| zhang | 127 | 0 | 0000000 | 0000001 | 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| li | -128 | 0 | 0000000 | 0000001 | 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| wang | 25 | 255 | 0000000 | 0000001 | 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| zhao | -128 | 0 | 0000000 | 0000001 | 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| qian | 0 | 0 | 0000000 | 0000001 | 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| li | 0 | 0 | 0000015 | 0000001 | 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| zhang | 127 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| li | -128 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| wang | 25 | 255 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| zhao | -128 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| qian | 0 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| li | 0 | 0 | 0000015 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| zhang | 127 | 0 | 0000000 | 0000001 | 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| li | -128 | 0 | 0000000 | 0000001 | 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| wang | 25 | 255 | 0000000 | 0000001 | 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| zhao | -128 | 0 | 0000000 | 0000001 | 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| qian | 0 | 0 | 0000000 | 0000001 | 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| li | 0 | 0 | 0000015 | 0000001 | 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| zhang | 127 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| zhang | 127 | 0 | 0000000 | 0000001 | 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhang | 127 | 0 | 0000000 | 0000001 | 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhang | 127 | 0 | 0000000 | 0000001 | 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhang | 127 | 0 | 0000000 | 0000001 | 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhang | 127 | 0 | 0000000 | 0000001 | 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
+——-+——+——-+———+———+—-+———+——-+——+———————+

mysql> select * from class,mytest2 where class.idnum=mytest2.idnum;

+——-+——+——-+———+———+—-+———+——-+——+———————+
| sname | age | score | snum | idnum | id | idnum | sale | sex | loginDate |
+——-+——+——-+———+———+—-+———+——-+——+———————+
| zhang | 127 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| li | -128 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| wang | 25 | 255 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| zhao | -128 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| qian | 0 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| li | 0 | 0 | 0000015 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| zhang | 127 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| li | -128 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| wang | 25 | 255 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| zhao | -128 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| qian | 0 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| li | 0 | 0 | 0000015 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| zhang | 127 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
+——-+——+——-+———+———+—-+———+——-+——+———————+

  • left join
mysql> select * from class left join mytest2 on class.idnum=mytest2.idnum;

+——-+——+——-+———+———+——+———+——-+——+———————+
| sname | age | score | snum | idnum | id | idnum | sale | sex | loginDate |
+——-+——+——-+———+———+——+———+——-+——+———————+
| zhang | 127 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| zhang | 127 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| zhang | 127 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| li | -128 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| li | -128 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| wang | 25 | 255 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| wang | 25 | 255 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| zhao | -128 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| zhao | -128 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| qian | 0 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| qian | 0 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| li | 0 | 0 | 0000015 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| li | 0 | 0 | 0000015 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
+——-+——+——-+———+———+——+———+——-+——+———————+

  • inner join
mysql> select * from class inner join mytest2 on class.idnum=mytest2.idnum;

+——-+——+——-+———+———+—-+———+——-+——+———————+
| sname | age | score | snum | idnum | id | idnum | sale | sex | loginDate |
+——-+——+——-+———+———+—-+———+——-+——+———————+
| zhang | 127 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| li | -128 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| wang | 25 | 255 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| zhao | -128 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| qian | 0 | 0 | 0000000 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| li | 0 | 0 | 0000015 | 0000001 | 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| zhang | 127 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| li | -128 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| wang | 25 | 255 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| zhao | -128 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| qian | 0 | 0 | 0000000 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| li | 0 | 0 | 0000015 | 0000001 | 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| zhang | 127 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| li | -128 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| wang | 25 | 255 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| zhao | -128 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| qian | 0 | 0 | 0000000 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| li | 0 | 0 | 0000015 | 0000001 | 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
+——-+——+——-+———+———+—-+———+——-+——+———————+

  • union
mysql> select * from mytest2;

+—-+———+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+———+——-+——+———————+
| 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
+—-+———+——-+——+———————+

  • mysql> select * from mytest5;

+—-+———+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+———+——-+——+———————+
| 1 | 0000001 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 0000002 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 0000001 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 0000005 | 90.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 0000001 | 50.00 | 0 | 2017-07-29 16:06:36 |
| 6 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 7 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 8 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 9 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 10 | 0000002 | 70.00 | 0 | 2017-07-29 16:06:36 |
+—-+———+——-+——+———————+

(1)union 去重

mysql> select * from mytest2 union select * from mytest5;

+—-+——-+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+——-+——-+——+———————+
| 1 | 1 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 2 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 1 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 5 | 90.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 1 | 50.00 | 0 | 2017-07-29 16:06:36 |
| 6 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 7 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 8 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 9 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 10 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
+—-+——-+——-+——+———————+

(2)union all 不去重

mysql> select * from mytest2 union all select * from mytest5;

+—-+——-+——-+——+———————+
| id | idnum | sale | sex | loginDate |
+—-+——-+——-+——+———————+
| 1 | 1 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 2 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 1 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 5 | 90.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 1 | 50.00 | 0 | 2017-07-29 16:06:36 |
| 6 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 7 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 8 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 9 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 10 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 1 | 1 | 10.00 | 0 | 2017-07-29 14:54:05 |
| 2 | 2 | 20.00 | 1 | 2017-07-29 14:54:05 |
| 3 | 1 | 13.00 | 0 | 2017-07-29 14:54:58 |
| 4 | 5 | 90.00 | 0 | 2017-07-29 14:54:58 |
| 5 | 1 | 50.00 | 0 | 2017-07-29 16:06:36 |
| 6 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 7 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 8 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 9 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
| 10 | 2 | 70.00 | 0 | 2017-07-29 16:06:36 |
+—-+——-+——-+——+———————+

  • abs()
mysql> select abs(2-5);

+———-+
| abs(2-5) |
+———-+
| 3 |
+———-+

  • bin()
mysql> select bin(7);

+——–+
| bin(7) |
+——–+
| 111 |
+——–+

  • floor()
mysql> select floor(3.2);

+————+
| floor(3.2) |
+————+
| 3 |
+————+

  • ceiling()
mysql> select ceiling(3.22);

+—————+
| ceiling(3.22) |
+—————+
| 4 |
+—————+

  • length()和char_length()
mysql> select length('张三');

+—————-+
| length(‘张三’) |
+—————-+
| 4 |
+—————-+

mysql> select char_length('张三');

+———————+
| char_length(‘张三’) |
+———————+
| 2 |
+———————+

  • reverse
mysql> select reverse('张三');

+—————–+
| reverse(‘张三’) |
+—————–+
| 三张 |
+—————–+

  • position
mysql> select position('@' in '123@');

+————————-+
| position(‘@’ in ‘123@’) |
+————————-+
| 4 |
+————————-+

  • right()
mysql> select right('abcdefg',3);

+——————–+
| right(‘abcdefg’,3) |
+——————–+
| efg |
+——————–+

  • now()
mysql> select now();

+———————+
| now() |
+———————+
| 2017-07-30 23:12:31 |
+———————+

  • curr_date()
mysql> select curdate();

+————+
| curdate() |
+————+
| 2017-07-30 |
+————+

  • curtime()
mysql> select curtime();

+———–+
| curtime() |
+———–+
| 23:13:17 |
+———–+

  • week()
mysql> select week(curdate());

+—————–+
| week(curdate()) |
+—————–+
| 31 |
+—————–+

  • md5();
mysql> select md5(111);

+———————————-+
| md5(111) |
+———————————-+
| 698d51a19d8a121ce581499d7b701668 |
+———————————-+

原创粉丝点击