zoukankan      html  css  js  c++  java
  • 常见的mysql数据库sql语句的编写和运行结果

    省份城市试题
    #省份表
        -> select * from province;
    +----+----------+
    | id | province |
    +----+----------+
    |  1 | 广东     |
    |  2 | 湖南     |
    |  3 | 湖北     |
    +----+----------+
    3 rows in set

    mysql> #城市表
        -> select * from city;
    +----+------+------------+
    | id | city | provinceid |
    +----+------+------------+
    |  1 | 广州 |          1 |
    |  2 | 深圳 |          1 |
    |  3 | 惠州 |          1 |
    |  4 | 长沙 |          2 |
    |  5 | 武汉 |          3 |
    +----+------+------------+
    #获得所有的城市并得出该城市的省
        -> select c.id,c.city,p.province from city c left join province p on c.provinceid=p.id;
    +----+------+----------+
    | id | city | province |
    +----+------+----------+
    |  1 | 广州 | 广东     |
    |  2 | 深圳 | 广东     |
    |  3 | 惠州 | 广东     |
    |  4 | 长沙 | 湖南     |
    |  5 | 武汉 | 湖北     |
    +----+------+----------+
    #获得所有省份,并查处该省份所有城市的个数
        -> select p.id,p.province,count(*) from city c left join province p on c.provinceid=p.id group by c.provinceid;
     
    +----+----------+----------+
    | id | province | count(*) |
    +----+----------+----------+
    |  1 | 广东     |        3 |
    |  2 | 湖南     |        1 |
    |  3 | 湖北     |        1 |
    +----+----------+----------+


    学生分数表

    mysql> select * from score;
    +----+----------+-------+-------+
    | id | username | class | score |
    +----+----------+-------+-------+
    |  1 | 张三     | 语文  |    81 |
    |  2 | 张三     | 数学  |    75 |
    |  3 | 李四     | 语文  |    76 |
    |  4 | 李四     | 数学  |    90 |
    |  5 | 王五     | 语文  |    81 |
    |  6 | 王五     | 数学  |   100 |
    |  7 | 王五     | 英语  |    90 |
    +----+----------+-------+-------+

    获得学生的总分数,平均分数
        -> select id,username,sum(score),avg(score) from score group by username;
    +----+----------+------------+------------+
    | id | username | sum(score) | avg(score) |
    +----+----------+------------+------------+
    |  1 | 张三     | 156        | 78.0000    |
    |  3 | 李四     | 166        | 83.0000    |
    |  5 | 王五     | 271        | 90.3333    |
    +----+----------+------------+------------+

    #获得所有分数都大于80的学生的名字
    select * from score group by username having min(score)>80;
    +----+----------+-------+-------+
    | id | username | class | score |
    +----+----------+-------+-------+
    |  5 | 王五     | 语文  |    81 |
    +----+----------+-------+-------+

    select distinct(username) from score where username not in (select username from score where score<80);
    +----------+
    | username |
    +----------+
    | 王五     |
    +----------+



    包含值
     select * from test;
    +----+---------+
    | id | data    |
    +----+---------+
    |  1 | 2       |
    |  2 | 1,2     |
    |  3 | 1,22,23 |
    |  4 | 2,34    |
    |  5 | 1,2,6   |
    +----+---------+
    5 rows in set
    获得2的所有数据,但不要22等
    mysql> select * from test where FIND_IN_SET(2,data);
    +----+-------+
    | id | data  |
    +----+-------+
    |  1 | 2     |
    |  2 | 1,2   |
    |  4 | 2,34  |
    |  5 | 1,2,6 |
    +----+-------+

  • 相关阅读:
    完爆!用边缘容器,竟能秒级实现团队七八人一周的工作量
    手把手教你使用 cert-manager 签发免费证书
    手把手教你使用 Nginx Ingress 实现金丝雀发布
    Codeforces 534B Covered Path 贪心
    Codeforces 534A Exam 水
    Topcoder open 2015 Round 1A 250 Similars 枚举 + 状压
    Topcoder SRM 654 DIV1 500 FoldingPaper2 递归 + 枚举
    Topcoder SRM655 DIV2 250 BichromeBoard 水
    2015 Google code jam Qualification Round B 枚举 + 贪心
    2015 Google code jam Qualification Round A 水
  • 原文地址:https://www.cnblogs.com/behindman/p/8733215.html
Copyright © 2011-2022 走看看