> DQL (Data Query Lanuage) (使用操作符对)数据进行分类
操作符是一个保留字或者字符,主要用于 DQL 的where 子语句。
1. 比较操作符
equal: =
not equal: <>
less than: <
larger than: >
other: <= >=
2. 逻辑操作符
IS NULL:用于和NULL比较
BETWEEN:用于寻找位于两个值之间的值, 和AND 配合一起用
IN :用于和一个指定的列表进行比较
LIKE:利用与通配符类似的值比较。 %表示零个、一个和多个字符;下划线——
EXISTS:用于搜索指定表里是否满足。。
UNIQUE:
ALL :用于和另个一个集合的全部值进行比较
SOME: ANY的别名
ANY:用于和列表中的任意值进行比较
3. 求反操作符
NOT EQUAL: <>, !=
NOT BETWEEN:
NOT IN:
NOT LIKE:
IS NOT NULL:
NOT EXISTS:
NOT UNIQUE:
4. 算符操作符
加:+
减:-
乘:*
除:/
5. 连接操作符
AND
OR
练习:
mysql> create table em_tbl
-> (id serial ,
-> name varchar(20) not null,
-> age decimal(4) null,
-> salary decimal(10, 2) null);
Query OK, 0 rows affected (0.08 sec)
mysql>
mysql> desc em_tbl;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | decimal(4,0) | YES | | NULL | |
| salary | decimal(10,2) | YES | | NULL | |
+--------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql>
mysql> insert into em_tbl
-> values( null, 'andrew', 25, 10000);
Query OK, 1 row affected (0.01 sec)
mysql> insert into em_tbl(name, age, salary)
-> values('ray', 30, 15000);
Query OK, 1 row affected (0.01 sec)
mysql> insert into em_tbl(name, age, salary)
-> values('jerry', 31, 14000);
Query OK, 1 row affected (0.01 sec)
mysql> insert into em_tbl(name, age, salary)
-> values('jane', 21, 9000);
Query OK, 1 row affected (0.02 sec)
mysql> insert into em_tbl(name, age, salary)
-> values('mengo', 40, 15000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into em_tbl(name, age, salary)
-> values('lei', 30, 13000);
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> insert into em_tbl(name, age, salary)
-> values('fei', 30, null);
Query OK, 1 row affected (0.01 sec)
mysql> insert into em_tbl(name, age, salary)
-> values('dan', 30, null);
Query OK, 1 row affected (0.01 sec)
mysql> insert into em_tbl(name, age, salary)
-> values('lin', null, 9999);
Query OK, 1 row affected (0.01 sec)
mysql> insert into em_tbl(name, age, salary)
-> values('wang', null, 8888);
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> delete from em_tbl
-> where name = 'dan'
-> and age = null;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> delete from em_tbl
-> where name = 'dan';
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> select * from em_tbl;
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 4 | jane | 21 | 9000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
| 7 | fei | 30 | NULL |
| 9 | lin | NULL | 9999.00 |
| 10 | wang | NULL | 8888.00 |
+----+--------+------+----------+
9 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where id = 1;
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
+----+--------+------+----------+
1 row in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where id <> 1;
+----+-------+------+----------+
| id | name | age | salary |
+----+-------+------+----------+
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 4 | jane | 21 | 9000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
| 7 | fei | 30 | NULL |
| 9 | lin | NULL | 9999.00 |
| 10 | wang | NULL | 8888.00 |
+----+-------+------+----------+
8 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where id < 3 ;
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
+----+--------+------+----------+
2 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where id >= 3 ;
+----+-------+------+----------+
| id | name | age | salary |
+----+-------+------+----------+
| 3 | jerry | 31 | 14000.00 |
| 4 | jane | 21 | 9000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
| 7 | fei | 30 | NULL |
| 9 | lin | NULL | 9999.00 |
| 10 | wang | NULL | 8888.00 |
+----+-------+------+----------+
7 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where salary is null;
+----+------+------+--------+
| id | name | age | salary |
+----+------+------+--------+
| 7 | fei | 30 | NULL |
+----+------+------+--------+
1 row in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where age is null;
+----+------+------+---------+
| id | name | age | salary |
+----+------+------+---------+
| 9 | lin | NULL | 9999.00 |
| 10 | wang | NULL | 8888.00 |
+----+------+------+---------+
2 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where age between 25 and 30;
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
| 7 | fei | 30 | NULL |
+----+--------+------+----------+
4 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where salary between 9000 and 15000;
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 4 | jane | 21 | 9000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
| 9 | lin | NULL | 9999.00 |
+----+--------+------+----------+
7 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where salary in (10000, 15000, 8888, 9999);
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
| 5 | mengo | 40 | 15000.00 |
| 9 | lin | NULL | 9999.00 |
| 10 | wang | NULL | 8888.00 |
+----+--------+------+----------+
5 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where salary like '1%';
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
+----+--------+------+----------+
5 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where salary like '%15%';
+----+-------+------+----------+
| id | name | age | salary |
+----+-------+------+----------+
| 2 | ray | 30 | 15000.00 |
| 5 | mengo | 40 | 15000.00 |
+----+-------+------+----------+
2 rows in set (0.00 sec)
mysql>
mysql> select * from em_tbl
-> where salary like '1%0';
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
+----+--------+------+----------+
5 rows in set (0.00 sec)
mysql>
mysql> select id, name, age
-> from em_tbl
-> where exists (select *
-> from em_tbl
-> where age <30 );
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | andrew | 25 |
| 2 | ray | 30 |
| 3 | jerry | 31 |
| 4 | jane | 21 |
| 5 | mengo | 40 |
| 6 | lei | 30 |
| 7 | fei | 30 |
| 9 | lin | NULL |
| 10 | wang | NULL |
+----+--------+------+
9 rows in set (0.00 sec)
mysql> select *
-> from em_tbl
-> WHERE salary in (10000, 13000, 14000);
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 3 | jerry | 31 | 14000.00 |
| 6 | lei | 30 | 13000.00 |
+----+--------+------+----------+
3 rows in set (0.00 sec)
mysql>
mysql> select *
-> from em_tbl
-> where salary > all ( select salary
-> from em_tbl
-> WHERE salary in (10000, 13000, 14000));
+----+-------+------+----------+
| id | name | age | salary |
+----+-------+------+----------+
| 2 | ray | 30 | 15000.00 |
| 5 | mengo | 40 | 15000.00 |
+----+-------+------+----------+
2 rows in set (0.00 sec)
mysql>
mysql> select *
-> from em_tbl
-> where salary >= all ( select salary
-> from em_tbl
-> WHERE salary in (10000, 13000, 14000));
+----+-------+------+----------+
| id | name | age | salary |
+----+-------+------+----------+
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 5 | mengo | 40 | 15000.00 |
+----+-------+------+----------+
3 rows in set (0.00 sec)
mysql>
mysql>
mysql> select *
-> from em_tbl
-> where salary >= any ( select salary
-> from em_tbl
-> WHERE salary in (10000, 13000, 14000));
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
+----+--------+------+----------+
5 rows in set (0.00 sec)
mysql>
mysql> select *
-> from em_tbl
-> where salary > any ( select salary
-> from em_tbl
-> WHERE salary in (10000, 13000, 14000));
+----+-------+------+----------+
| id | name | age | salary |
+----+-------+------+----------+
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
+----+-------+------+----------+
4 rows in set (0.00 sec)
mysql>
mysql>
mysql> select *
-> from em_tbl
-> where age is not null
-> and salary is not null;
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 1 | andrew | 25 | 10000.00 |
| 2 | ray | 30 | 15000.00 |
| 3 | jerry | 31 | 14000.00 |
| 4 | jane | 21 | 9000.00 |
| 5 | mengo | 40 | 15000.00 |
| 6 | lei | 30 | 13000.00 |
+----+--------+------+----------+
6 rows in set (0.00 sec)
mysql>
mysql> select age + id
-> from em_tbl;
+----------+
| age + id |
+----------+
| 26 |
| 32 |
| 34 |
| 25 |
| 45 |
| 36 |
| 37 |
| NULL |
| NULL |
+----------+
9 rows in set (0.00 sec)
mysql>
mysql> select (age + id*10)
-> from em_tbl;
+---------------+
| (age + id*10) |
+---------------+
| 35 |
| 50 |
| 61 |
| 61 |
| 90 |
| 90 |
| 100 |
| NULL |
| NULL |
+---------------+
9 rows in set (0.00 sec)
mysql>
mysql> select salary * 1.05 + age*100
-> from em_tbl;
+-------------------------+
| salary * 1.05 + age*100 |
+-------------------------+
| 13000.0000 |
| 18750.0000 |
| 17800.0000 |
| 11550.0000 |
| 19750.0000 |
| 16650.0000 |
| NULL |
| NULL |
| NULL |
+-------------------------+
9 rows in set (0.00 sec)
mysql>
mysql>
mysql>