zoukankan      html  css  js  c++  java
  • DQL 数据查询语言

    下面用到的包已放到文件里需要的可以自己下载

    1.select 简单查询命令

    #1.查询表中所有的数据
    mysql> select * from test.student;
    
    #2.查看所有数据之前,先查看数据量
    mysql> select count(*) from test.student;
    
    #3.查询指定列
    mysql> select user,host from mysql.user;
    
    #4.按条件查询
    mysql> select * from test.student where id='8';
    mysql> select id,name from test.student where id='8';

    2.查询数据测试

    1)将sql导入数据库

    #上传sql文件到服务器
    [root@db01 ~]# rz world.sql
    
    #导入sql到数据库
    mysql> source /root/world.sql;
    mysql> . /root/world.sql

    2)查询的操作

    #1.查看库下面的表
    mysql> show tables from world;
    
    mysql> use world
    mysql> show tables;
    +-----------------+
    | Tables_in_world |
    +-----------------+
    | city            |
    | country         |
    | countrylanguage |
    +-----------------+
    3 rows in set (0.00 sec)
    
    #2.查看表结构
    mysql> desc city;
    
    #3.查询所有数据
    mysql> select count (*) from city;
    mysql> select * from city;
    
    #4.查询指定列数据
    mysql> select name,population from city;
    
    #5.按照人口数量排序
    #升序
    mysql> select name,population from city order by population;
    #降序
    mysql> select name,population from city order by population desc;
    
    #6.查看人口数量最多排名前十的城市
    mysql> select name,population from city order by population desc limit 10;
    
    #7.按照步长查询数据
    #查询数据从10后面开始计算,展示20条数据,20就是步长
    mysql> select id,name,population from city limit 10,20;
    
    mysql> select id,name,population from city limit 0,60;
    mysql> select id,name,population from city limit 60,60;
    mysql> select id,name,population from city limit 120,60;

    3.按条件查询

    #1.条件查询where的符号
    where的条件符号: = < > >= <= != <>
    where的连接符:and or like in
    
    #2.查看中国城市的人口数量
    mysql> select CountryCode,name,population from city where CountryCode='CHN';
    
    #3.查看黑龙江省城市的人口数量
    mysql> select CountryCode,District,name,population from city where CountryCode='CHN' and District='heilongjiang';
    
    #4.查询中国人口数量小于10万的城市
    mysql> select CountryCode,population,name from city where CountryCode='CHN' and population<'100000';
    
    #5.查看国家代码以H开头的
    mysql> select * from city where CountryCode like 'H%';
    
    #6.查看国家代码以H结尾的
    mysql> select * from city where CountryCode like '%H';
    
    #7.查看国家代码包含H的
    mysql> select * from city where CountryCode like '%H%';
    
    #8.查询中国城市和美国城市的人口数量
    mysql> select CountryCode,name,population from city where CountryCode='CHN' or CountryCode='USA';
    mysql> select CountryCode,name,population from city where CountryCode in ('CHN','USA');
    
    #9.联合查询
    mysql> select CountryCode,name,population from city where CountryCode='CHN' union all select CountryCode,name,population from city where CountryCode='USA';

    二、select 高级用法(多表联查,连表查询)

    1.传统连接

    1)查询题1:

    #查询世界上小于100人的城市是哪个国家的?
    
    #1.审题:查看需要查询哪些数据
    城市名字   城市人口数量   国家名字
    
    #2.找到查询内容的字段在哪个表
    城市名字        城市人口数量          国家名字
    city.name      city.population        country.name
    
    #3.找出两个表中关联的列
    city.countrycode
    country.code
    
    #4.编写语句
    select city.name,city.population,country.name from city,country where city.countrycode=country.code  and city.population < '100';
    
    select city.name,city.population,country.name from city natural join country where city.population < '100';

    2)多表联查练习题2:

    #查询世界上小于100人的城市是哪个国家的,使用什么语言?
    
    #1.审题:查看需要查询哪些数据
    城市名字   城市人口数量   国家名字   国家的语言
    
    #2.找到查询内容的字段在哪个表
    城市名字        城市人口数量          国家名字        国家的语言
    city.name      city.population        country.name    countrylanguage.language
    
    #3.找出三个表相关联的列
    city.countrycode
    country.code
    countrylanguage.CountryCode
    
    #4.编写语句
    select city.name,city.population,country.name,countrylanguage.language from city,country,countrylanguage where city.countrycode=country.code and country.code=countrylanguage.CountryCode and city.population < '100';

     

    2.自连接

    #查询人口数量大于100万的城市,列出他们的国家代码和国家语言
    
    1.传统连接:
    select city.name,city.population,countrylanguage.CountryCode,countrylanguage.language from city,countrylanguage where countrylanguage.CountryCode=city.CountryCode and city.population > '1000000';
    
    2.自连接:
    select city.name,city.population,countrylanguage.CountryCode,countrylanguage.language from city natural join countrylanguage where city.population > '1000000';
    
    #注意:
        1.自连接会自动去获取两个表之间的关联列和数据,所以自连接的两个表必须有相同的字段和数据

    3.内连接

    1)语法

    select * from 表1 join 表2 on 关联条件 where 条件
    
    #注意:
        表 1 是小表
        表 2 是大表

    2)例子:

    #查询世界上小于100人的城市是哪个国家的,国家代码是什么
    
    1.传统链接:
    select city.population,city.name,country.name,country.code from city,country where country.code=city.countrycode and city.population < '100';
    
    2.内连接:
    select city.population,city.name,country.name,country.code from country join city on country.code=city.countrycode where city.population < '100';

    3)内连接三表联查

    #查询世界上小于100人的城市是哪个国家的,用什么语言?
    select city.population,city.name,country.name,countrylanguage.language from country join city on city.countrycode=country.code join countrylanguage on country.code=countrylanguage.countrycode where city.population < '100';

    4.外连接

    1)左外连接

    select city.name,city.countrycode,country.name 
    from city left join country 
    on city.countrycode=country.code 
    and city.population<100;

    2)右外连接

    select city.name,city.countrycode,country.name 
    from city right join country 
    on city.countrycode=country.code 
    and city.population<100;

    5.UNION(合并查询)

    #范围查询OR语句
    mysql> select * from city where countrycode='CHN' or countrycode='USA';
    
    #范围查询IN语句
    mysql> select * from city where countrycode in ('CHN','USA');
    
    #替换为:
    mysql> select * from city where countrycode='CHN' 
    union all
    select * from city where countrycode='USA' limit 10;
  • 相关阅读:
    随机森林算法参数调优
    BAYES和朴素BAYES
    阿里云 金融接口 token PHP
    PHP mysql 按时间分组 表格table 跨度 rowspan
    MySql按周,按月,按日分组统计数据
    PHP 获取今日、昨日、本周、上周、本月的等等常用的起始时间戳和结束时间戳的时间处理类
    thinkphp5 tp5 会话控制 session 登录 退出 检查检验登录 判断是否应该跳转到上次url
    微信 模板消息
    php 腾讯 地图 api 计算 坐标 两点 距离 微信 网页 WebService API
    php添加http头禁止浏览器缓存
  • 原文地址:https://www.cnblogs.com/chenlifan/p/13907246.html
Copyright © 2011-2022 走看看