MySQL基础教程7——DQL—基础数据查询

315 阅读1分钟

MySQL基础教程7——DQL—基础数据查询

基本查询

使用select 字段名 from 表名;

mysql> select id,name from user;
+------+------+
| id   | name |
+------+------+
|    1 | Tom  |
+------+------+

多个字段名之间用,隔开,如果要查询表中所有字段可以用*代替字段名。

条件查询

使用select 字段名 from 表名 where 条件

符号作用
大于>
小于<
等于=
不等于!=或者<>(推荐使用这种)
大等于>=
小等于<=

and条件

用于连接两条件,例如:

select * from user where id = 1 and name = 'Tom'; 

or条件

用于连接两条件:例如

select * from user where id = 1 or telephone = '15006451568';

in/not in条件

用于判断该值是否存在该字段存储值中,例如:

select * from user where id age (not) in (1,2,3);

is null/is not null条件

空值查询,例如:

select * from user where name is (not) null;

between and条件

区间查询,例如:

select * from user where age between 18 and 32;

like条件

模糊查询,例如:

select * from user where name like "____";

四条下划线表示查询名字为四个字段的人,一条_代表一个字符,一个%代表任意个字符,查询林姓且为三个字的人用林__,查询电话号码是135开头的用135%,当然也可以用_补全电话号码位数,但是这样过于麻烦了。

distinct条件

去重查询,例如:

select distinct * from user 

(点击进入专栏查看详细教程)