mysql 

数据类型

mysql> CREATE DATABASE db1;
mysql> USE db1;
Database changed

mysql> create table zx1(tiny tinyint,small smallint,med mediumint,tni int, big bigint);

mysql> INSERT INTO zx1 VALUES(1234567890,1234567890,1234567890,1234567890,1234567890);

Query OK, 1 row affected, 3 warnings (0.01 sec)

mysql> show warnings;
+---------+------+------------------------------------------------+
| Level   | Code | Message                                        |
+---------+------+------------------------------------------------+
| Warning | 1264 | Out of range value for column 'tiny' at row 1  |
| Warning | 1264 | Out of range value for column 'small' at row 1 |
| Warning | 1264 | Out of range value for column 'med' at row 1   |
+---------+------+------------------------------------------------+

mysql> select * from zx1;
+------+-------+---------+------------+------------+
| tiny | small | med     | tni        | big        |
+------+-------+---------+------------+------------+
|  127 | 32767 | 8388607 | 1234567890 | 1234567890 |
+------+-------+---------+------------+------------+

浮点型
mysql> CREATE TABLE fdx1(flo FLOAT(6,3));
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO fdx1 VALUES(1.234),(123.234),(1232456.123);
Query OK, 3 rows affected, 1 warning (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 1

mysql> SHOW WARNINGS;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1264 | Out of range value for column 'flo' at row 3 |
+---------+------+----------------------------------------------+

mysql> select * from fdx1;
+---------+
| flo     |
+---------+
|   1.234 |
| 123.234 |
| 999.999 |
+---------+
3 rows in set (0.00 sec)

无符号:  unsigned

mysql> create table zx2(id tinyint unsigned);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into zx2 values(1234567890);
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1264 | Out of range value for column 'id' at row 1 |
+---------+------+---------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from zx2;
+------+
| id   |
+------+
|  255 |
+------+
--------------------------
字符型
char  固定长度的字符类型

mysql> create table ch1(name char(10));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into ch1 values("abcdefghijklmn");
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from ch1;
+------------+
| name       |
+------------+
| abcdefghij |
+------------+

varchar 可变长度字符类型  

mysql> create table va1(name varchar(1000));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into va1 values("abcdefghijklmn");
Query OK, 1 row affected (0.00 sec)

mysql> select * from va1;
+----------------+
| name           |
+----------------+
| abcdefghijklmn |
+----------------+
1 row in set (0.00 sec)

---------------------
enum 和 set 类型
enum: 互斥,从一组数据列表中,选择一个数据

mysql> create table enu1(sex enum("M","W"));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into enu1 values("M");
Query OK, 1 row affected (0.01 sec)

mysql> insert into enu1 values("W");
Query OK, 1 row affected (0.00 sec)

mysql> insert into enu1 values("MW");
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select * from enu1;
+------+
| sex  |
+------+
| M    |
| W    |
|      |
+------+
3 rows in set (0.00 sec)

set  集合类型;可以从一组数据中取多个值

mysql> create table se1(work set("network","windows","linux"));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into se1 values("network,windows");
Query OK, 1 row affected (0.00 sec)

mysql> select * from se1;
+-----------------+
| work            |
+-----------------+
| network,windows |
+-----------------+
1 row in set (0.00 sec)

mysql> insert into se1 values("network,windows,linux");
Query OK, 1 row affected (0.00 sec)

mysql> select * from se1;
+-----------------------+
| work                  |
+-----------------------+
| network,windows       |
| network,windows,linux |
+-----------------------+

----------------------------------------
时间和日期类型

date  ‘1000-01-01’  --- ‘9999-12-31’
       2012-02-31   ---> 0000-00-00

mysql> create table date1(rq date);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into date1 values("2013-09-02");
Query OK, 1 row affected (0.00 sec)

mysql> insert into date1 values("20130902");
Query OK, 1 row affected (0.00 sec)

mysql> insert into date1 values("2013-04-31");
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+-----------------------------------------+
| Level   | Code | Message                                 |
+---------+------+-----------------------------------------+
| Warning | 1265 | Data truncated for column 'rq' at row 1 |
+---------+------+-----------------------------------------+
1 row in set (0.00 sec)

mysql> select * from date1;
+------------+
| rq         |
+------------+
| 2013-09-02 |
| 2013-09-02 |
| 0000-00-00 |
+------------+

time

mysql> create table time1(sj time);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into time1 values("12:11:12");
Query OK, 1 row affected (0.00 sec)

mysql> insert into time1 values("10 12:11:12");
Query OK, 1 row affected (0.00 sec)

mysql> insert into time1 values("11:11");
Query OK, 1 row affected (0.00 sec)

mysql> select * from time1;
+-----------+
| sj        |
+-----------+
| 12:11:12  |
| 252:11:12 |
| 11:11:00  |
+-----------+

year 
    2位     0-69   2001-2069
           70-99   1970-1999
    4位     1901-2155

mysql> create table year1(nian year);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into year1 values("70"),("69"),("00"),("1983"),("2013");

mysql> create table year1(nian year);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into year1 values("70"),("69"),("00"),("1983"),("2013");
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from year1;
+------+
| nian |
+------+
| 1970 |
| 2069 |
| 2000 |
| 1983 |
| 2013 |
+------+

datetime

‘1000-01-01 00:00:00’  --- ‘9999-12-31 23:59:59’

mysql> create table da2(dt datetime);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into da2 values("2013-09-02 11:29:00");
Query OK, 1 row affected (0.00 sec)

mysql> select * from da2;
+---------------------+
| dt                  |
+---------------------+
| 2013-09-02 11:29:00 |
+---------------------+

timestamp

-------------------------------
DML
增         删        改        查
insert   delete    update   select

1)增
添加数据-->表

mysql> create table z1(id int,name varchar(12));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into z1 values(1,"liuyi");
Query OK, 1 row affected (0.01 sec)

mysql> insert into z1(id) values(2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from z1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | liuyi |
|    2 | NULL  |
+------+-------+

2)删

mysql> delete from z1;
Query OK, 2 rows affected (0.01 sec)

mysql> insert into z1 values(1,"liuyi"),(2,"maer");
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from z1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | liuyi |
|    2 | maer  |
+------+-------+
2 rows in set (0.00 sec)

mysql> delete from z1 where name="maer";
Query OK, 1 row affected (0.00 sec)

mysql> select * from z1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | liuyi |
+------+-------+

mysql> delete from z1 where name="liuyi";
Query OK, 1 row affected (0.01 sec)

mysql> select * from z1;
Empty set (0.00 sec)

mysql> insert into z1 values(1,"liuyi"),(2,"maer");
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into z1(id) values(3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into z1(id) values(4);
Query OK, 1 row affected (0.01 sec)

mysql> delete from z1 where name is NULL;
Query OK, 2 rows affected (0.01 sec)

mysql> select * from z1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | liuyi |
|    2 | maer  |
+------+-------+
2 rows in set (0.00 sec)

mysql> delete from z1 where name is not NULL;
Query OK, 2 rows affected (0.01 sec)

mysql> select * from z1;
Empty set (0.00 sec)
-------

mysql> insert into z1 values(1,"liuyi"),(2,"maer");
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into z1(id) values(3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into z1(id) values(4);
Query OK, 1 row affected (0.00 sec)

mysql> select * from z1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | liuyi |
|    2 | maer  |
|    3 | NULL  |
|    4 | NULL  |
+------+-------+

mysql> truncate z1;
Query OK, 0 rows affected (0.01 sec)

3)改

mysql> insert into z1 values(1,"liuyi"),(2,"maer"),(3,"niusan"),(4,"niusi");
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from z1;
+------+--------+
| id   | name   |
+------+--------+
|    1 | liuyi  |
|    2 | maer   |
|    3 | niusan |
|    4 | niusi  |
+------+--------+
4 rows in set (0.01 sec)

mysql> update z1 set id=9;
Query OK, 4 rows affected (0.02 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from z1;
+------+--------+
| id   | name   |
+------+--------+
|    9 | liuyi  |
|    9 | maer   |
|    9 | niusan |
|    9 | niusi  |
+------+--------+

mysql> update z1 set id=1 where name="liuyi";

mysql> select * from z1;
+------+--------+
| id   | name   |
+------+--------+
|    1 | liuyi  |
|    9 | maer   |
|    9 | niusan |
|    9 | niusi  |
+------+--------+

4)select
mysql> create table shop(id int,name varchar(50),price float(6,2),street varchar(30),city varchar(30),pdate date,tel char(13));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into shop values(1,".1 liyi",5.99,"sanhaojie","shenyang","2010-01-01","024-31500816");

mysql> insert into shop values(2,".2 lier",6.99,"sanhaojie","shenyang","2010-01-11","024-31500817");

mysql> insert into shop values(3,".3 lisan",9.99,"sanhaojie","shenyang","2010-01-21","024-31500818");

mysql> insert into shop values(4,"5 wangwu",2.39,"taiyuanjie","shenyang","2010-02-02","024-41500816");

mysql> insert into shop values(5,"6 wangwu",5.50,"taiyuanjie","shenyang","2010-02-12","024-41500817");

mysql> insert into shop values(6,"7 wangqi",15.50,"taiyuanjie","shenyang","2010-02-20","024-41500818");

mysql> insert into shop values(7,"zhaoqi",10.50,"zhongjie","shenyang","2011-03-01","024-51500816");

mysql> insert into shop values(8,"zhaoba",50.50,"zhongjie","shenyang","2011-03-09","024-51500817");

mysql> insert into shop values(9,"zhaojiu",20.00,"zhongjie","shenyang","2011-03-19","024-51500819");

mysql> insert into shop values(10,"ZHAOJIU",25.00,"changanjie","beijing","2011-05-19","010-11500819");

mysql> insert into shop values(11,"ZHAOBA",35.00,"changanjie","beijing","2011-05-29","010-11500818");

mysql> select * from shop;
+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    1 | .1 liyi  |  5.99 | sanhaojie  | shenyang | 2010-01-01 | 024-31500816 |
|    2 | .2 lier  |  6.99 | sanhaojie  | shenyang | 2010-01-11 | 024-31500817 |
|    3 | .3 lisan |  9.99 | sanhaojie  | shenyang | 2010-01-21 | 024-31500818 |
|    4 | 5 wangwu |  2.39 | taiyuanjie | shenyang | 2010-02-02 | 024-41500816 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
|    6 | 7 wangqi | 15.50 | taiyuanjie | shenyang | 2010-02-20 | 024-41500818 |
|    7 | zhaoqi   | 10.50 | zhongjie   | shenyang | 2011-03-01 | 024-51500816 |
|    8 | zhaoba   | 50.50 | zhongjie   | shenyang | 2011-03-09 | 024-51500817 |
|    9 | zhaojiu  | 20.00 | zhongjie   | shenyang | 2011-03-19 | 024-51500819 |
|   10 | ZHAOJIU  | 25.00 | changanjie | beijing  | 2011-05-19 | 010-11500819 |
|   11 | ZHAOBA   | 35.00 | changanjie | beijing  | 2011-05-29 | 010-11500818 |
+------+----------+-------+------------+----------+------------+--------------+

mysql> select id from shop;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
|    7 |
|    8 |
|    9 |
|   10 |
|   11 |
+------+

mysql> select id,name from shop;
+------+----------+
| id   | name     |
+------+----------+
|    1 | .1 liyi  |
|    2 | .2 lier  |
|    3 | .3 lisan |
|    4 | 5 wangwu |
|    5 | 6 wangwu |
|    6 | 7 wangqi |
|    7 | zhaoqi   |
|    8 | zhaoba   |
|    9 | zhaojiu  |
|   10 | ZHAOJIU  |
|   11 | ZHAOBA   |
+------+----------+

mysql> select distinct name from shop;  //distinct 去掉重复的行。

+----------+
| name     |
+----------+
| .1 liyi  |
| .2 lier  |
| .3 lisan |
| 5 wangwu |
| 6 wangwu |
| 7 wangqi |
| zhaoqi   |
| zhaoba   |
| zhaojiu  |
+----------+

mysql> select id from shop limit 0,1;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> select id from shop limit 1,3;
+------+
| id   |
+------+
|    2 |
|    3 |
|    4 |
+------+

排序
order by

mysql> select id,price from shop order by price;
+------+-------+
| id   | price |
+------+-------+
|    4 |  2.39 |
|    5 |  5.50 |
|    1 |  5.99 |
|    2 |  6.99 |
|    3 |  9.99 |
|    7 | 10.50 |
|    6 | 15.50 |
|    9 | 20.00 |
|   10 | 25.00 |
|   11 | 35.00 |
|    8 | 50.50 |
+------+-------+
11 rows in set (0.00 sec)

mysql> select id,price from shop order by price desc;
+------+-------+
| id   | price |
+------+-------+
|    8 | 50.50 |
|   11 | 35.00 |
|   10 | 25.00 |
|    9 | 20.00 |
|    6 | 15.50 |
|    7 | 10.50 |
|    3 |  9.99 |
|    2 |  6.99 |
|    1 |  5.99 |
|    5 |  5.50 |
|    4 |  2.39 |
+------+-------+

根据指定条件查询

mysql> select id,name from shop where id=1;
+------+---------+
| id   | name    |
+------+---------+
|    1 | .1 liyi |
+------+---------+

mysql> select * from shop where id=1;
+------+---------+-------+-----------+----------+------------+--------------+
| id   | name    | price | street    | city     | pdate      | tel          |
+------+---------+-------+-----------+----------+------------+--------------+
|    1 | .1 liyi |  5.99 | sanhaojie | shenyang | 2010-01-01 | 024-31500816 |
+------+---------+-------+-----------+----------+------------+--------------+

mysql> select * from shop where city="shenyang";
+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    1 | .1 liyi  |  5.99 | sanhaojie  | shenyang | 2010-01-01 | 024-31500816 |
|    2 | .2 lier  |  6.99 | sanhaojie  | shenyang | 2010-01-11 | 024-31500817 |
|    3 | .3 lisan |  9.99 | sanhaojie  | shenyang | 2010-01-21 | 024-31500818 |
|    4 | 5 wangwu |  2.39 | taiyuanjie | shenyang | 2010-02-02 | 024-41500816 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
|    6 | 7 wangqi | 15.50 | taiyuanjie | shenyang | 2010-02-20 | 024-41500818 |
|    7 | zhaoqi   | 10.50 | zhongjie   | shenyang | 2011-03-01 | 024-51500816 |
|    8 | zhaoba   | 50.50 | zhongjie   | shenyang | 2011-03-09 | 024-51500817 |
|    9 | zhaojiu  | 20.00 | zhongjie   | shenyang | 2011-03-19 | 024-51500819 |
+------+----------+-------+------------+----------+------------+--------------+

比较运算符
= > < >= <= != <>  "between 值1 and 值2"

mysql> select * from shop where id>3;

+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    4 | 5 wangwu |  2.39 | taiyuanjie | shenyang | 2010-02-02 | 024-41500816 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
|    6 | 7 wangqi | 15.50 | taiyuanjie | shenyang | 2010-02-20 | 024-41500818 |
|    7 | zhaoqi   | 10.50 | zhongjie   | shenyang | 2011-03-01 | 024-51500816 |
|    8 | zhaoba   | 50.50 | zhongjie   | shenyang | 2011-03-09 | 024-51500817 |
|    9 | zhaojiu  | 20.00 | zhongjie   | shenyang | 2011-03-19 | 024-51500819 |
|   10 | ZHAOJIU  | 25.00 | changanjie | beijing  | 2011-05-19 | 010-11500819 |
|   11 | ZHAOBA   | 35.00 | changanjie | beijing  | 2011-05-29 | 010-11500818 |
+------+----------+-------+------------+----------+------------+--------------+

mysql> select * from shop where id!=3;

mysql> select * from shop where id<>3;

mysql> select * from shop where id=3;

mysql> select * from shop where id between 3 and 5;
+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    3 | .3 lisan |  9.99 | sanhaojie  | shenyang | 2010-01-21 | 024-31500818 |
|    4 | 5 wangwu |  2.39 | taiyuanjie | shenyang | 2010-02-02 | 024-41500816 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
+------+----------+-------+------------+----------+------------+--------------+

mysql> select * from shop where price between 20 and 50;
+------+---------+-------+------------+----------+------------+--------------+
| id   | name    | price | street     | city     | pdate      | tel          |
+------+---------+-------+------------+----------+------------+--------------+
|    9 | zhaojiu | 20.00 | zhongjie   | shenyang | 2011-03-19 | 024-51500819 |
|   10 | ZHAOJIU | 25.00 | changanjie | beijing  | 2011-05-19 | 010-11500819 |
|   11 | ZHAOBA  | 35.00 | changanjie | beijing  | 2011-05-29 | 010-11500818 |
+------+---------+-------+------------+----------+------------+--------------+

逻辑运算符
and  or  not

mysql> select * from shop where id>3 and price<50;
+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    4 | 5 wangwu |  2.39 | taiyuanjie | shenyang | 2010-02-02 | 024-41500816 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
|    6 | 7 wangqi | 15.50 | taiyuanjie | shenyang | 2010-02-20 | 024-41500818 |
|    7 | zhaoqi   | 10.50 | zhongjie   | shenyang | 2011-03-01 | 024-51500816 |
|    9 | zhaojiu  | 20.00 | zhongjie   | shenyang | 2011-03-19 | 024-51500819 |
|   10 | ZHAOJIU  | 25.00 | changanjie | beijing  | 2011-05-19 | 010-11500819 |
|   11 | ZHAOBA   | 35.00 | changanjie | beijing  | 2011-05-29 | 010-11500818 |
+------+----------+-------+------------+----------+------------+--------------+

mysql> select * from shop where id>6 or id<3;
+------+---------+-------+------------+----------+------------+--------------+
| id   | name    | price | street     | city     | pdate      | tel          |
+------+---------+-------+------------+----------+------------+--------------+
|    1 | .1 liyi |  5.99 | sanhaojie  | shenyang | 2010-01-01 | 024-31500816 |
|    2 | .2 lier |  6.99 | sanhaojie  | shenyang | 2010-01-11 | 024-31500817 |
|    7 | zhaoqi  | 10.50 | zhongjie   | shenyang | 2011-03-01 | 024-51500816 |
|    8 | zhaoba  | 50.50 | zhongjie   | shenyang | 2011-03-09 | 024-51500817 |
|    9 | zhaojiu | 20.00 | zhongjie   | shenyang | 2011-03-19 | 024-51500819 |
|   10 | ZHAOJIU | 25.00 | changanjie | beijing  | 2011-05-19 | 010-11500819 |
|   11 | ZHAOBA  | 35.00 | changanjie | beijing  | 2011-05-29 | 010-11500818 |
+------+---------+-------+------------+----------+------------+--------------+

mysql> select * from shop where not (id>=6 or id<=3);
+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    4 | 5 wangwu |  2.39 | taiyuanjie | shenyang | 2010-02-02 | 024-41500816 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
+------+----------+-------+------------+----------+------------+--------------+

mysql> select * from shop where (id>6 or id<3) and price<20;
+------+---------+-------+-----------+----------+------------+--------------+
| id   | name    | price | street    | city     | pdate      | tel          |
+------+---------+-------+-----------+----------+------------+--------------+
|    1 | .1 liyi |  5.99 | sanhaojie | shenyang | 2010-01-01 | 024-31500816 |
|    2 | .2 lier |  6.99 | sanhaojie | shenyang | 2010-01-11 | 024-31500817 |
|    7 | zhaoqi  | 10.50 | zhongjie  | shenyang | 2011-03-01 | 024-51500816 |
+------+---------+-------+-----------+----------+------------+--------------+

in

mysql> select * from shop where id in (1,3,5,7,9);
+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    1 | .1 liyi  |  5.99 | sanhaojie  | shenyang | 2010-01-01 | 024-31500816 |
|    3 | .3 lisan |  9.99 | sanhaojie  | shenyang | 2010-01-21 | 024-31500818 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
|    7 | zhaoqi   | 10.50 | zhongjie   | shenyang | 2011-03-01 | 024-51500816 |
|    9 | zhaojiu  | 20.00 | zhongjie   | shenyang | 2011-03-19 | 024-51500819 |
+------+----------+-------+------------+----------+------------+--------------+

mysql> select * from shop where street in ("sanhaojie","taiyuanjie");
+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    1 | .1 liyi  |  5.99 | sanhaojie  | shenyang | 2010-01-01 | 024-31500816 |
|    2 | .2 lier  |  6.99 | sanhaojie  | shenyang | 2010-01-11 | 024-31500817 |
|    3 | .3 lisan |  9.99 | sanhaojie  | shenyang | 2010-01-21 | 024-31500818 |
|    4 | 5 wangwu |  2.39 | taiyuanjie | shenyang | 2010-02-02 | 024-41500816 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
|    6 | 7 wangqi | 15.50 | taiyuanjie | shenyang | 2010-02-20 | 024-41500818 |
+------+----------+-------+------------+----------+------------+--------------+

NULL
    is null
    is not null

mysql> select * from shop where name is not null;
+------+----------+-------+------------+----------+------------+--------------+
| id   | name     | price | street     | city     | pdate      | tel          |
+------+----------+-------+------------+----------+------------+--------------+
|    1 | .1 liyi  |  5.99 | sanhaojie  | shenyang | 2010-01-01 | 024-31500816 |
|    2 | .2 lier  |  6.99 | sanhaojie  | shenyang | 2010-01-11 | 024-31500817 |
|    3 | .3 lisan |  9.99 | sanhaojie  | shenyang | 2010-01-21 | 024-31500818 |
|    4 | 5 wangwu |  2.39 | taiyuanjie | shenyang | 2010-02-02 | 024-41500816 |
|    5 | 6 wangwu |  5.50 | taiyuanjie | shenyang | 2010-02-12 | 024-41500817 |
|    6 | 7 wangqi | 15.50 | taiyuanjie | shenyang | 2010-02-20 | 024-41500818 |
|    7 | zhaoqi   | 10.50 | zhongjie   | shenyang | 2011-03-01 | 024-51500816 |
|    8 | zhaoba   | 50.50 | zhongjie   | shenyang | 2011-03-09 | 024-51500817 |
|    9 | zhaojiu  | 20.00 | zhongjie   | shenyang | 2011-03-19 | 024-51500819 |
|   10 | ZHAOJIU  | 25.00 | changanjie | beijing  | 2011-05-19 | 010-11500819 |
|   11 | ZHAOBA   | 35.00 | changanjie | beijing  | 2011-05-29 | 010-11500818 |
+------+----------+-------+------------+----------+------------+--------------+

mysql> select * from shop where city is null;
+------+------+-------+--------+------+-------+------+
| id   | name | price | street | city | pdate | tel  |
+------+------+-------+--------+------+-------+------+
|   12 | NULL |  NULL | NULL   | NULL | NULL  | NULL |
+------+------+-------+--------+------+-------+------+

模糊查询
   like
   通配符
   %  字符出现任意次数
   _  单个字符


mysql> select name from shop where name like "%i%";
+----------+
| name     |
+----------+
| .1 liyi  |
| .2 lier  |
| .3 lisan |
| 7 wangqi |
| zhaoqi   |
| zhaojiu  |
| ZHAOJIU  |
+----------+

mysql> select name from shop where name like "%i";
+----------+
| name     |
+----------+
| .1 liyi  |
| 7 wangqi |
| zhaoqi   |
+----------+

mysql> select * from shop where name like "i%";
Empty set (0.00 sec)

mysql> select name from shop where name like "zhao__";
+--------+
| name   |
+--------+
| zhaoqi |
| zhaoba |
| ZHAOBA |
+--------+

mysql> select name from shop where name like "zhao___";
+---------+
| name    |
+---------+
| zhaojiu |
| ZHAOJIU |
+---------+

mysql> show variables like "%cache%";

mysql> select name from shop where name like "zhao%";
+---------+
| name    |
+---------+
| zhaoqi  |
| zhaoba  |
| zhaojiu |
| ZHAOJIU |
| ZHAOBA  |
+---------+

正则表达式
regexp
. * ^ $ + ? | x{m} x{m,} x{m,n} [[:<:]] [[:>:]] \\

mysql> select  name from shop where name regexp "^\\.";
+----------+
| name     |
+----------+
| .1 liyi  |
| .2 lier  |
| .3 lisan |
+----------+

mysql> select name from shop where name regexp "^z";
+---------+
| name    |
+---------+
| zhaoqi  |
| zhaoba  |
| zhaojiu |
| ZHAOJIU |
| ZHAOBA  |
+---------+

mysql> select name from shop where name regexp "i$";
+----------+
| name     |
+----------+
| .1 liyi  |
| 7 wangqi |
| zhaoqi   |
+----------+
3 rows in set (0.00 sec)

mysql> select name from shop where name regexp "i[[:>:]]"; 
+----------+
| name     |
+----------+
| .1 liyi  |
| 7 wangqi |
| zhaoqi   |
+----------+

mysql> select id from shop where id like "1";
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> select id from shop where id regexp "1";
+------+
| id   |
+------+
|    1 |
|   10 |
|   11 |
|   12 |
+------+

使用函数来查询
concat()

mysql> select concat(city,"_",street) from shop;
+-------------------------+
| concat(city,"_",street) |
+-------------------------+
| shenyang_sanhaojie      |
| shenyang_sanhaojie      |
| shenyang_sanhaojie      |
| shenyang_taiyuanjie     |
| shenyang_taiyuanjie     |
| shenyang_taiyuanjie     |
| shenyang_zhongjie       |
| shenyang_zhongjie       |
| shenyang_zhongjie       |
| beijing_changanjie      |
| beijing_changanjie      |
| NULL                    |
+-------------------------+

mysql> select concat("商品",name,"电话是",tel) from shop;
+---------------------------------------+
| concat("商品",name,"电话是",tel)      |
+---------------------------------------+
| 商品.1 liyi电话是024-31500816         |
| 商品.2 lier电话是024-31500817         |
| 商品.3 lisan电话是024-31500818        |
| 商品5 wangwu电话是024-41500816        |
| 商品6 wangwu电话是024-41500817        |
| 商品7 wangqi电话是024-41500818        |
| 商品zhaoqi电话是024-51500816          |
| 商品zhaoba电话是024-51500817          |
| 商品zhaojiu电话是024-51500819         |
| 商品ZHAOJIU电话是010-11500819         |
| 商品ZHAOBA电话是010-11500818          |
| NULL                                  |
+---------------------------------------+

as 别名

mysql> select concat("商品",name,"电话是",tel) as info from shop;
+-------------------------------------+
| info                                |
+-------------------------------------+
| 商品.1 liyi电话是024-31500816       |
| 商品.2 lier电话是024-31500817       |
| 商品.3 lisan电话是024-31500818      |
| 商品5 wangwu电话是024-41500816      |
| 商品6 wangwu电话是024-41500817      |
| 商品7 wangqi电话是024-41500818      |
| 商品zhaoqi电话是024-51500816        |
| 商品zhaoba电话是024-51500817        |
| 商品zhaojiu电话是024-51500819       |
| 商品ZHAOJIU电话是010-11500819       |
| 商品ZHAOBA电话是010-11500818        |
| NULL                                |
+-------------------------------------+

mysql> select concat(city,"_",street) as address from shop;
+---------------------+
| address             |
+---------------------+
| shenyang_sanhaojie  |
| shenyang_sanhaojie  |
| shenyang_sanhaojie  |
| shenyang_taiyuanjie |
| shenyang_taiyuanjie |
| shenyang_taiyuanjie |
| shenyang_zhongjie   |
| shenyang_zhongjie   |
| shenyang_zhongjie   |
| beijing_changanjie  |
| beijing_changanjie  |
| NULL                |
+---------------------+
12 rows in set (0.00 sec)

mysql> select street as "街道" from shop;
+------------+
| 街道       |
+------------+
| sanhaojie  |
| sanhaojie  |
| sanhaojie  |
| taiyuanjie |
| taiyuanjie |
| taiyuanjie |
| zhongjie   |
| zhongjie   |
| zhongjie   |
| changanjie |
| changanjie |
| NULL       |
+------------+

lower()  upper()

mysql> select lower(name) from shop;
+-------------+
| lower(name) |
+-------------+
| .1 liyi     |
| .2 lier     |
| .3 lisan    |
| 5 wangwu    |
| 6 wangwu    |
| 7 wangqi    |
| zhaoqi      |
| zhaoba      |
| zhaojiu     |
| zhaojiu     |
| zhaoba      |
| NULL        |
+-------------+

mysql> select upper(name) from shop;
+-------------+
| upper(name) |
+-------------+
| .1 LIYI     |
| .2 LIER     |
| .3 LISAN    |
| 5 WANGWU    |
| 6 WANGWU    |
| 7 WANGQI    |
| ZHAOQI      |
| ZHAOBA      |
| ZHAOJIU     |
| ZHAOJIU     |
| ZHAOBA      |
| NULL        |
+-------------+

用于汇总
count()  sum()  max()  min()  avg()

mysql> select count(id) from shop;
+-----------+
| count(id) |
+-----------+
|        12 |
+-----------+

mysql> select count(name) from shop;
+-------------+
| count(name) |
+-------------+
|          11 |
+-------------+

mysql> select count(distinct name) from shop;
+----------------------+
| count(distinct name) |
+----------------------+
|                    9 |
+----------------------+

mysql> select sum(price) from shop;
+------------+
| sum(price) |
+------------+
|     187.36 |
+------------+

算术运算符
+ - * / %

mysql> select sum(price*10) from shop;
+---------------+
| sum(price*10) |
+---------------+
|       1873.60 |
+---------------+

mysql> select sum(price+2) from shop;
+--------------+
| sum(price+2) |
+--------------+
|       209.36 |
+--------------+

mysql> select max(price) from shop;
+------------+
| max(price) |
+------------+
|      50.50 |
+------------+

mysql> select min(price) from shop;
+------------+
| min(price) |
+------------+
|       2.39 |
+------------+


mysql> select avg(price) from shop;+------------+
| avg(price) |
+------------+
|  17.032727 |
+------------+

时间和日期函数
date()
time()
year()
month()
day()
hour()
minute()
second()

mysql> select year(pdate),month(pdate),day(pdate) from shop;

+-------------+--------------+------------+
| year(pdate) | month(pdate) | day(pdate) |
+-------------+--------------+------------+
|        2010 |            1 |          1 |
|        2010 |            1 |         11 |
|        2010 |            1 |         21 |
|        2010 |            2 |          2 |
|        2010 |            2 |         12 |
|        2010 |            2 |         20 |
|        2011 |            3 |          1 |
|        2011 |            3 |          9 |
|        2011 |            3 |         19 |
|        2011 |            5 |         19 |
|        2011 |            5 |         29 |
|        NULL |         NULL |       NULL |
+-------------+--------------+------------+

其他函数

mysql> select database();
+------------+
| database() |
+------------+
| db1        |
+------------+

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2013-09-02 16:35:32 |
+---------------------+
1 row in set (0.00 sec)

-------------------------------
分组查询
group by 列名

mysql> select city,count(*) from shop group by city;
+----------+----------+
| city     | count(*) |
+----------+----------+
| NULL     |        1 |
| beijing  |        2 |
| shenyang |        9 |
+----------+----------+

mysql> create table d2(id int,name char(2),sex enum("M","W"));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into d2 values(1,"a","M"),(2,"b","W"),(3,"c","M"),(4,"d","M"),(5,"e","W");

mysql> select * from d2;
+------+------+------+
| id   | name | sex  |
+------+------+------+
|    1 | a    | M    |
|    2 | b    | W    |
|    3 | c    | M    |
|    4 | d    | M    |
|    5 | e    | W    |
+------+------+------+

mysql> help GROUP_CONCAT;

mysql> select sex,group_concat(id),group_concat(name) from d2 group by sex;
+------+------------------+--------------------+
| sex  | group_concat(id) | group_concat(name) |
+------+------------------+--------------------+
| M    | 1,3,4            | a,c,d              |
| W    | 2,5              | b,e                |
+------+------------------+--------------------+

mysql> select sex,count(sex) from d2 group by sex having count(sex)>2;
+------+------------+
| sex  | count(sex) |
+------+------------+
| M    |          3 |
+------+------------+

mysql> select city,count(city) from shop where price>=10.00 group by city;
+----------+-------------+
| city     | count(city) |
+----------+-------------+
| beijing  |           2 |
| shenyang |           4 |
+----------+-------------+

mysql> select city,count(city) from shop where price>=10.00 group by city having count(city)>3;
+----------+-------------+
| city     | count(city) |
+----------+-------------+
| shenyang |           4 |
+----------+-------------+

书写顺序
select from where group by having order by limit

执行顺序
from where group by having select order by limit