Molet

MySQL常用sql语句—–数据表的查询操作

Molet 安全防护 2023-02-26 480浏览 0

 

1.查询指定字段
select c_id,c_age,c_name from t_student;
select c_id as 编号,c_name as 姓名,c_age 性别 from t_student;

2.去重查询
select distinct c_address from t_student
select distinct c_address,c_name from t_student

3.比较运算符查询
select c_name,c_gender from t_student where c_gender =”女 “;
select c_id ,c_name,c_age,c_gender from t_student where c_age <>18;

4.逻辑运算符查询
select c_name,c_age,c_gender from t_student where c_gender = “女” and c_age <20;
select c_name,c_age from t_student where c_age =18 or 20;   # 有问题.会产生注入问题
select c_name ,c_age from t_student where not c_age = 18;   # not一般用在非空的地方

5.模糊查询
like
%: 表示任意多个字符
_: 表示任意一个字符
select c_id,c_name from t_student where c_name like “孙”;
select c_id,c_name from t_student where c_name like “孙_”;
select c_id,c_name from t_student where c_name like “%三%”;

6.范围查询
in:非连续 / between:连续
select c_id,c_name from t_student where c_id in (1,5,8,23,7,12,100000);
select c_name,c_age from t_student where c_age between 18 and 20; # 包括18和20

7.判空
select c_name,c_age from t_student where c_age is Null;
select c_name,c_age from t_student where c_age is not Null;

8.查询结果排序 order by
select c_name ,c_id,c_age from t_student order by c_age; # 默认升序
select c_name ,c_id,c_age from t_student where c_gender = “女” order by c_age desc; # 降序

9.分页查询
select c_id ,c_name from t_student limit 10,10; # 从11开始 20结束
select c_id ,c_name from t_student limit 30,10; # 从31开始 40结束
— 每页显示2个,第3个页面
select * from students limit 4,2;
— 每页显示m个,第n个页面
select * from students limit   m(n-1),m;

10.聚合函数 # 很少单独用,一般结合分组一起用
select sum(c_age) from t_student;
select sum(c_age),avg(c_age),max(c_age),min(c_age),count(*) from t_stuudent where c_gender = “女”

11.分组:
  1. where….group by….having: 先经过where大筛选,再.group by分组,再对分组的结果用having限制查询
  2.单分组没有意义,相当于去重,往往结合:group_concat(c_name)使用
  3.having中like查询不再适用,只能配合group by 使用
单字段分组:
  select c_gender from t_student group by c_gender;
多字段分组: having 对group分组后进行筛选
  select c_address,c_gender from t_student group by c_gender,c_address;
  select c_address,c_gender,group_concat(c_name) from t_student group by c_gender,c_address   having c_gender = “女”; <====>select c_address,c_gender,group_concat(c_name) from t_student   where c_gender =”女”group by c_gender,c_address;

例1:mysql> select c_address,c_name from t_student group by c_address,c_name having c_gender = “女”;
ERROR 1054 (42S22): Unknown column ‘c_gender’ in ‘having clause’
注意1:having用在group by分组后,但是分组后并没有 c_gender这一字段

12.多表查询
没外键的时候:不会连接c_class_id为空的数据
select s.c_name,c.c_name from t_student as s,t_class as c where s.c_class_id = c.c_id;

  1.内连接:on
  ”””注意:数据库默认为内连接,也可以不写inner join 用,隔开2个表但是连接方式需要用where
    但是不能再接where了

    即:t_a , t_b where 连接关系; <=====> t_a inner join t_b on 连接关系 [+where]
    这里有where=情况下先执行where语句
  ”””
    select s.c_name,c.c_name from t_student s inner join t_class c on s.c_class_id = c.c_id where c_gender = “女”;

  2.左连接:以左表为基准
    select s.c_name ,c.c_name from t_student s left join t_class c on s.c_class_id = c.c_id;

  3.右连接
    select s.c_name,c.c_name from t_student s right join t_class c on s.c_class_id =c.c_id;

    4.适用场景

     左A表  右B表

       如果 A>B且无包含关系 :right join
       如果 A<B且无包含关系 :left join
       如果 A>B且有包含关系 :inner join
       如果 A<B且有包含关系 :inner join

13.标量子查询
  select c_name,c_age from t_student where c_age > (select avg(c_age) from t_student);

13-1列级子查询: 结果为一列 (主查询 where 条件 in )
例:查询所有学生所在班级的班级名称
第一步:抽取关键字:所有学生—->student表, 班级名称—->class表
第二步:在student表中找出所有学生所在的教室id:select c_class_id from t_student
第三步:在class中根据这些教室id找出对应的教室名字(class表中可能不止这几个教室)
  select c_name from t_class where c_id in (select c_class_id from t_student);

13-2行级子查询 : 结果为一行 (主查询 where (字段1,2,…) = (行子查询))
例:查找班级里 年龄最大 且 所在班号最小 的学生
第一步:抽取关键字 :年龄最大—–>student表 , 所在班号最小的学生—>student表
第二步:选出最大年龄的学生和最小的班号:select max(c_age),min(c_class_id) from t_student
第三步:找出年龄和班号满足条件的学生 :
如果所有学生中年龄最大的在2班,则找回的结果为空
  select * from t_student where (c_age,c_class_id) = (select max(c_age),min(c_class_id) from t_student);  # (c_age ,c_class_id )中的顺序后后面select语句限制
<======>select * from t_student where (c_age = (select max(c_age) from t_student)) and (c_class_id = (select min(c_class_id) from t_student));

14.自连接查询–省–市–区
在查询数据时,只有一张表,查询时使用自己连接自己。 语法: select * from 表1 inner join 表2 on 表1.列 运算符 表2.列 where 条件;
1.查询一共有多少个省
select count(*) from areas where pid is null;

2.查询山西省的所有城市=====>要用到where 所以要用inner join ..on 语法
第一步:拆分表:省表(字段:aid ,atitle) 市表(字段:aid ,atitle,pid)
第二步:找出省表(p)中的山西省
第三步:分析表可知,省表中的aid = 市表(c)中的cid 即有所属关系,并不是简单的aid = pid
select c.* from areas as c inner join areas as p on c.pid = p.aid where p.atitle =”山西省”; # 必须是p.atitle

3.查询市的名称为“广州市”的所有区县
select qu.* from areas as qu inner join areas as c on c.aid = qu.pid where c.atitle =’广州市’;

继续浏览有关 数据库技术文章/教程 的文章
发表评论