文章目录
- 一、基本用法
- 二、查询语句
- 三、别名
- 四、进阶查询(where)
- 1.条件查询
- 2.区间查询
- 3.模糊查询
- 4.in查询
- 5.坑
- 五、辅助查询
- 1.排序
- 继续扯个蛋
一、基本用法
select 字段1,字段2... from 表名;
select * from 表名;
二、查询语句
# 1.查询常量
select 'name';
# 2.表达式
select 1+3;
select 1/3;
select 'a'+'b'; # 结果是0
select 'a'+1; # 结果是1
# 3.函数
select mod(20,3) # 求余数
select 20%3 # 同上
# 创建表
create table student(
sid char(8) primary key,
sname varchar(20),
age int default 0
);
show tables;
desc student;
insert into student values
(值1,值2);
三、别名
作用就是简化
# 1.列的别名
select 字段1 [as] 字段1别名,
字段2 [as] 字段2别名,
字段3 [as] 字段3别名
from 表名;
# 举例
select sid as '学号',
sname [as] '姓名'
from student;
# 2.表的别名
select 字段 from 表名 表别名;
# 举例
select s.sid from student s;
# 3.多表查询
select * from 表1,表2;
四、进阶查询(where)
1.条件查询
# 语法
select 列名 from 表名 where 条件;
# 运算符
1.等于 =
2.不等于 <>
3.逻辑与 and
4.逻辑或 or
2.区间查询
# 查询年龄30-35之间的人 and 左边小右边大
select * from student where age between 30 and 35;
3.模糊查询
select 列名 from 表名 where 列 like pattern;
# 例子 查询姓张的学生
select * from student where sname like '张%';
4.in查询
select 列名 from 表名 where 列名 in (值1,值2,值3);
select 列名 from 表名 where 列名 not in (值1,值2,值3);
5.坑
五、辅助查询
1.排序
# 默认升序
select 字段名 from 表名 order by 字段1 [asc|desc], 字段2 [asc|desc];
继续扯个蛋