查询学生表所有字段:select * from 表名;
查询学生表所有字段:select 字段名1,字段名2 from 表名;
给查询结果的列起别名:select 字段名1 as 别名,字段名2 as 别名 from 表名;(也可以给表起别名和字段同理)
as可以省略!!!
函数concat(a,b):拼接作用
去重(distinct):select distinct 字段名 from 表名;
查询版本号:select version();
用于计算:select 计算公式;
查看自增的步长:select @@auto_increment_increment;
所有结果+1分:(只修改查询结果,原来表里的数值不变)
条件查询:select 字段名 from 表名 where 条件判断(可以有多个条件,逗号隔开);
逻辑运算符
运算符 | 语法 | 描述 |
and && | a and b a && b | 逻辑与 |
or || | a or b a || b | 逻辑或 |
not ! | not a ! b | 逻辑非 |
between and | 闭合区间 |
模糊查询:
运算符 | 语法 | 描述 |
is null | a is null | 如果字段数据为空,则为真 |
is not null | a is not null | 如果字段数据不为空,则为真 |
between and | c between a and b | 若c在a和b之间,则为真 |
like | a like b | sql匹配如果a匹配b,则为真 |
in | a in (a1,a2,a3) | 假设a在a1或者a2某个值中,则为真 |
like:
‘’%”代表0到任意个字符
‘’_‘’代表一个字符
in:(括号里是一个或多个具体值)
正则匹配:
REGEXP BINARY:表示不忽略大小写
REGEXP:表示忽略大小写
select 字段名 from 表名 where username REGEXP BINARY '正则语句'
例如:
select username, name, age, birthday from member where username REGEXP BINARY '^j'
查询只会返回j开头的
select username, name, age, birthday from member where username REGEXP '^j'
查询会返回j和J开头的数据