mysql基础_查询

完整的select语句
    select distinct 列1,列2(*表全部)
    from 表名
    where ....
    group by ... having ...
    order by ...
    limit start,count
执行顺序为:
    from 表名
    where ....
    group by ...
    select distinct *
    having ...
    order by ...
    limit start,count
实际使用中,只是语句中某些部分的组合,而不是全部

查询的完整格式:

SELECT select_expr [,select_expr,...] [      
      FROM tb_name
      [WHERE 条件判断]
      [GROUP BY {col_name | postion} [ASC | DESC], ...] 
      [HAVING WHERE 条件判断]
      [ORDER BY {col_name|expr|postion} [ASC | DESC], ...]
      [ LIMIT {[offset,]rowcount | row_count OFFSET offset}]

]

准备表 students和才classes:

 create table students(
        id int unsigned primary key auto_increment not null,
        name varchar(20) default '',
        age tinyint unsigned default 0,
        height decimal(5,2),
        gender enum('男','女','中性','保密') default '保密',
        cls_id int unsigned default 0,
        is_delete bit default 0

     );

 create table classes (
         id int unsigned auto_increment primary key not null,
        name varchar(30) not null

     );

添加数据

基础查询:select  列1,列2 [*表示所有信息] from 表明;    select * from students;

完整写法:select python_test_1.students.* from students;(select 库.表.列 from 库.表;)(通常在本库中不写库,就一个表时列前不加表明,当出现多张表时列前需要添加表明)

查看数据 并设置别名:select 列1 as 别名1 ,列2 as 别名2 (*表示所有信息)from 表明;      select id as 序号,name as 名字 from classes;

查询并去重:select distinct 列 from 表 ;    select distinct gender from students;(当distinct后有多列时当且仅当每列数据都重复是才去重)

条件查询(where):select 列 from 表 where 条件;   

        比较运算符:大于(>)小于(<)不等于(<>/!=)大于等于(>=)小于等于(<=)

     select * from students where id<4;

        逻辑运算符: 和(and) 或(or) 非(not):

        select * from students where id<2 or id>12;

模糊查询:like (%匹配任意个数字符,_匹配单个字符) select 列 from 表 where 条件列 like 字符;   

    select * from students where name like "_杰%";

范围查询: in(在列举范围内的数据) select 列 from 表 where 条件列 in (列举范围);    select * from students where id in(1,3);

            not in (不在列举范围内)    select * from students where id not in(1,2);(查询id不等于1,2的数据)

            between....and...(在一定范围以内的数据) select 列 from 表 where 条件列 between 开始范围 and 结束范围; 

select * from students where id between 2 and 5;

        not between...and...(不在某个范围以内的数据):select 列 from 表 where 条件列 not between 开始位置 and 结束位置;

判断为空 null(需要用is链接): select 列 from 表 where 条件列 is null;    select * from students where height is null;

判断不为空 not null(同样使用is链接):select 列 from 表 where 条件列 is not null;[select 列 from 表where not 条件列 is null;]    select * from students where height is not null;[select * from students where not height is null;]

排序 order by(默认从小到大,从小到大asc,从大到小desc):select 列 from 表order by 排序列 asc[desc];    

select * from students order by id desc;(按id从小到大排序,只写排序列不写asc/desc表示按默认排序)(若多个条件按从左到右条件优先级依次降低)

聚合函数(做统计使用)

    统计总数(count()):count(*)以行位单位统计个数,count(列)统计该列不为空的个数;select count(列) from 表;select count(height) from students;

    与条件使用用于统计符合该条件的总人数;select count(列) from 表 where 条件;

select count(height) from students where gender="男";

    max(列)->查询该最大值;min(列)->查询该最小值;select max(列)/min(列) from 表;    select max(age) from students;

    sum(列)->求和,avg(列)->求平均值;语法同上    select sum(age) from students;

    round(值,小数位n)做四舍五入并保留n位小数(通常与其他函数一起使用);select round(值,小数位) from 表;

select round(avg(height),1) from students;

分组操作(group dy)为了进行聚合统计(通常与其他集合函数一起使用);select 需要分组的列 from 表 group by 需要分组的列;(group_concat(数据)将数据拼接并输出为字符串):select 被分组列 from 表 group_concat 被分组列;

select gender from students group by gender;

select gender,group_concat(name) from students group by gender;

select gender,group_concat(name,":",height) from students group by gender;(统计同一性别的人的姓名和身高)

对统计后数据进一步筛选(having):select 被分组列,其他函数(其他函数可省略) from 表 group by 被分组列 having 筛选条件(筛选条件只能是分组中的条件)

select gender, group_concat(name) from students group by gender having gender = 1;(1表示该枚举类型中第一个变量)

where 和having的区别:where对源数据做筛选操作,having对分组后的数据做筛选操作.

分页:limit start,count(从第start条开始显示count条数据):select 列 from 表 limit start,count;

(start的默认值为0,可以省略)select 列 from 表 limit count;

 select * from students limit 0,3;

链接查询将两个表按某种条件合并到一起(条件查询时where在链接后的两个表算一个表所以where在链接之后)

连接查询 将两个表中的数据按照设置的连接条件进行筛选, 符合连接条件的数据才能够被筛选出来

 inner join 内链接->表1 inner join 表2 on 内连接条件;    select 列 from 表1 inner join 表2 on 链接条件;

select * from students inner join classes on students.cls_id = classes.id;(显示时表1 在前表2在后)

left join 左外连接查询->表1 left join 表2 on 链接条件:select 列 from 表1 left join 表2 on 链接条件;

select * from students left join classes on students.cls_id = classes.id;(表2 没有与表1对应数据是使用null填充)

right join 右外连接查询  使用的比较少:用法与左外链接用法相同;(表1 没有与表2对应的数据是使用null填充)

扩充了解, 内连接和外连接的其他写法
内连接的其他写法
select * from students join classes on students.cls_id = classes.id;
select * from students cross join classes on students.cls_id = classes.id;
外连接的其他写法

select * from students left outer join classes on students.cls_id = classes.id;(左链接)

自关联查询(适用于上下级关系的数据在同一张表中)

    表1 inner join 表1:建表:create table areas(aid int primary key,atitle varchar(20),pid int);(aid存自己编号,atitle存数据,pid存上一级数据)

select * from areas as a join in areas as p on a.aid = p.pid where a.atitle="数据1";(查询数据1 的下一级的所有数据)

子查询:select.....select......:select 列 from 表 where (select 列 from 表);即子查询结果作为主查询结果的条件的一部分

    需要使用小括号,小括号中的查询语句优先执行;

    子查询语句是一个可以单独执行的sql语句;

select * from students where age=(select max(age) from students);

错误之处欢迎指出

猜你喜欢

转载自blog.csdn.net/jlb1024/article/details/80889355
今日推荐