mysql运行结果图_常见的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 |

+----+-------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值