美文网首页
DQL 针对数据行的查询语句

DQL 针对数据行的查询语句

作者: 蔺蔺蔺赫赫 | 来源:发表于2019-07-19 08:39 被阅读0次

    select

        作用:获取MySQL中的数据行

    单独使用select:

        select @@xxx  查询参数信息

                  mysql> select @@port;

      mysql> show variables like '%innodb%';

        select 函数  mysql> select database(); 查看当前所在的库

                          mysql> select now();      查看当前时间

                          mysql> select version();  查看当前数据库版本

        select语法执行顺序

    select开始:至少要配合一个from

                ↓ 例子:1.use 数据库名;  2.select *from city;适合表数据行比较少的  3.

    from自句

                ↓

    where子句

                ↓

    group by子句

                ↓

    select后执行条件

                ↓

    having子句

                ↓

    order by

                ↓

    limit

      select开始:至少要配合一个from

                ↓ 例子1:1.use 数据库名; 

                            2.select *from city;适合表数据行比较少的 

          例子2: 查询name列 和 population列所有的值

          select name,population from world.city;

      where子句:

                例子1:where 配合 等值查询

      查询city 表中 中国城市信息

      select * from world.city where countrycode='CHN';  从 world库的city表中  查询 关于 CHN 的

    查询city 表中 美国城市信息  

      select * from world.city where countrycode='USA';  从 world库的city表中  查询 关于 USA 的

                例子2:where 配合 不等值查询< >  <=  >=  <>

    查询世界人口小于100人的城市

    select *  from  world.city  where population<100;  从 world库的city表中  查询 关于 人口<100 的

    查询世界人口大于10000000人的城市

    select *  from  world.city  where population>10000000;  从 world库的city表中  查询 关于 人口>10000000 的

                例子2:where 配合 模糊查询(like)

        查询国家代号是C的城市

    select *  from  world.city  where countrycode like 'C%';

    注意!!! like语句中 不要出现%在前面的情况 因为效率很低 不走索引

                例子3:where 配合 逻辑连接符查询(AND  OR)

        查询城市人口1万 到 2万之间的

        select * from world.city  where population > 10000 and population < 20000;

    从 world库的city表中  查询 关于 10000<人口< 20000 的

    查询中国 或 美国的城市信息

    select *   from  world.city where countrycode='CHN' or countrycode='USA';

    select *  from  world.city  where countrycode in ('CHN','USA');

    建议吧以上两种 改写为下边的这种  因为这样效率比较快

    select *  from  world.city  where countrycode='CHN'

    union [all]

    select *  from  world.city  where countrycode='USA';

      group by子句:配合聚合函数应用

     常用聚合函数

    平均 AVG()

    个数 count()

    总和 SUM()

    最大 MAX()

    最小 MIN()

    集合 GROUP_CONCAT()

    例子1:统计每个国家的总人口

    select countrycode,SUM(population) from city group by countrycode;

    例子2:统计每个国家的城市个数

    select countrycode,count(id) from city group by countrycode;

    从city 这个表中 以国家为基准 统计国家城市的个数

    1. 拿什么为基准

                          GROUP BY  countrycode

                    2. 统计什么东西

                          城市id,name

                    3. 统计东西的种类是什么?

                          COUNT(id)

    例子3:统计每个国家的省名字列表

    SELECT countrycode,GROUP_CONCAT(district) 

    FROM city

    GROUP BY countrycode;

    从city 这个表中 以国家为基准 统计国家 和 城市名称的统计

    例子4:统计中国每个省的城市名列表

    select District,GROUP_CONCAT(NAME)

    from city

    where countrycode='CHN'

    GROUP BY district;

    从city  这个表中  以城市为基准  统计城市和名称 关于中国的

    例子5:统计中国每个省的总人口数

    select district,SUM(population)

    from city

    where countrycode='CHN'

    GROUP BY district;

    从city  这个表中  以城市为基准 统计城市和人口总数 关于中国的

    HAVING    比较条件

            --- 统计中国,每个省的总人口大于1000w的省及人口数

            SELECT  district ,SUM(population) FROM city

            WHERE countrycode='CHN'

            GROUP BY district

            HAVING  SUM(population)>10000000

            说明: having后的条件是不走索引的,可以进行一些优化手段处理。

        ORDER BY  排序 从小到大  从大到小

            SELECT  district ,SUM(population)

    FROM city

            WHERE countrycode='CHN'

            GROUP BY district

            ORDER BY SUM(population) DESC  ;

            --- 例子:查询中国所有的城市,并以人口数降序输出

            SELECT *

    FROM city

    WHERE countrycode='CHN'

    ORDER BY  population DESC;人口数从大到小输出

        LIMIT

            SELECT *

            FROM city

            WHERE countrycode='CHN'

            ORDER BY  population DESC

            LIMIT 5;  显示5行

            SELECT *

            FROM city

            WHERE countrycode='CHN'

            ORDER BY  population DESC

            LIMIT 10; 显示10行

            SELECT *

            FROM city

            WHERE countrycode='CHN'

            ORDER BY  population DESC

            LIMIT 5,3;跳过头5行 显示随后3行

            LIMIT M,N    跳过M行,显示N行

    相关文章

      网友评论

          本文标题:DQL 针对数据行的查询语句

          本文链接:https://www.haomeiwen.com/subject/kwyrlctx.html