MySQL 自学笔记之:数据库查询 DQL(Data Query Lanuage)数据分类(where 子语句)

> 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> <span style="color:#ff0000;">create table em_tbl</span>
    -> <span style="color:#ff0000;">(id serial ,</span>
    -> <span style="color:#ff0000;">name varchar(20) not null,</span>
    -> <span style="color:#ff0000;">age decimal(4)  null,</span>
    -> <span style="color:#ff0000;">salary decimal(10, 2) null);</span>
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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值