MySQL 命令整理2

查询:

select * from t_student;

查询数据表中的指定字段数据:

select c_name, c_gender from t_student;

别名查询:

select c_name as 姓名, c_gender as 性别 from t_student;
select c_name Name, c_gender Gender from t_student;

消除重复结果:

select distinct c_adress from t_student;
select distinct c_name,c_adress from t_student(多个字段同时重复时才满足去重条件)

带条件where查询:

select * from t_student where c_gender='';
select c_name from t_student where c_id > 10;(可以使用不显示的字段作为筛选条件)


1.比较运算符查询:

类型 运算符
等于 =
大于 >
小于 <
大于等于 >=
小于等于 <=
不等于 <> or !=
并且 and
或者 or
not
select * from t_student where c_age < 20;

2.逻辑运算符:

select * from t_student where c_age < 20 and c_gender = '';

3.模糊查询

  查询符
模糊查询 like
查询任意个任意字符 %
查询一个任意字符 _
select * from t_student where c_name like '';
select * from t_student where c_name like '孙%';
select * from t_student where c_name like '孙_';

4.范围查询
a.使用in查询非连续范围

扫描二维码关注公众号,回复: 1845573 查看本文章
select * from t_student where c_id in(1,3,8);
select * from t_student where c_id = 1 or c_id = 3 or c_id = 8; 

b.使用between...and...查询连续区间

select * from t_student where c_age between 20 and 50;

5.空判断
a.判断空值

select * from t_student where c_age is null;

b.判断非空值

select * from t_student where c_age is not null;

排序
1.升/降序排列:

select * from t_student order by c_age asc(升序)/desc(降序)

2.多字段排序:

select * from t_student order by c_gender,c_age desc;

分页查询:

select * from t_student limit 3;
select * from t_student limit 3,3;

聚合函数
1.sum 求和函数

select sum(c_age) from t_student;

2.avg 求平均值

select avg(c_age) from t_student where c_gender='';

3.max 求最大值

select max(c_age) from t_student where c_gender='';

4.min 求最小值

select min(c_age) from t_student where c_gender='';

5.count 统计记录总数

select count(*) from t_student where c_gender='';

分组:
1.单字段分组:

select c_gender t_student group by c_gender;

2.多字段分组:

select c_gender,c_address group by c_gender,c_address;

3.分组后其它字段信息显示:

select c_gender,c_address,group_concat(c_name) from t_student group by c_gender,c_address;

4.分组与聚合函数组合使用:

select c_gender,max(c_age),min(c_age),sum(c_age),avg(c_age),count(*) from t_student group by c_gender;
select c_gender,max(c_age),min(c_age),sum(c_age),avg(c_age),count(c_age) from t_student group by c_gender;(使用字段方式时count不统计为空的值)

5.having条件:

select c_gender,group_concat(c_name) from t_student group by c_gender having c_gender='';

多重筛选条件:

select c_gender,group_concat(c_name) from t_student where c_age > 50 group by c_gender having c_gender='';

6.分组汇总

select c_gender,count(*) from t_student group by c_gender with rollup;

多表查询
1.普通多表查询

select * from t_student,t_class;(使用笛卡尔积交叉配对,该结果无意义)

2.条件多表查询

select t_student.c_name,t_class.c_name from t_student,t_class where t_student.c_class_id = t_class.c_id;

3.表别名

select ts.c_name '学生名',tc.c_name '班级名' from t_student ts,t_class tc where ts.c_class_id = tc.c_id;

4.内连接查询

select ts.c_name,tc.c_name from t_student ts inner join t_class tc where ts.c_class_id = tc.c_id;
select ts.c_name,tc.c_name from t_student ts inner join t_class tc on ts.c_class_id=tc.c_id where ts.c_id=1;

5.左连接查询

select ts.c_name,tc.c_name from t_student ts left join t_class tc where ts.c_class_id = tc.c_id;

6.右连接查询

select ts.c_name,tc.c_name from t_student ts right join t_class tc where ts.c_class_id = tc.c_id;


一般不使用右连接,将where后的判断条件调换位置即可实现与左连接相反的判断;

子查询
外部select语句成为主查询;
主查询与子查询之间的关系:
子查询是嵌入到主查询中
子查询是辅助主查询的,要么充当条件,要么充当数据源;
子查询是可以独立存在的语句,是一条完整的select语句。 

标量子查询
查询年纪大于平均年纪的学生姓名

select * from t_student where c_age >(select avg(c_age) from t_student);

列级子查询:
查询班级人员不为零的班级信息

select * from t_class where c_id in(select c_class_id from t_student);

行政子查询
查找班级年龄最大,所在班号最小的学生

select * from t_student where (c_age,c_class_id) = (select max(c_age),min(c_class_id) from t_student);

自连接查询
自连接意义:
在同一个表格保存具有关联性的单行简单数据

查询省份数量

select count(*) from areas where pid is null;

查询广东省的所有城市

select city.* from areas as city inner join areas as province on city.pid = province.aid where province.atitle = '广东省';

查询广州市的区域

select direction.* from areas as direction inner join areas as city on direction.pid = city.aid where city.atitle = '广州市';

猜你喜欢

转载自www.cnblogs.com/aaxwhxwh/p/9256931.html