示例及练习
1-MOSHOU.hero.txt
create database MOSHOU;
use MOSHOU;
create table hero(
id int,
name char(15),
sex enum("男","女"),
country char(10)
);
insert into hero values
(1,"曹操","男","魏国"),
(2,"小乔","女","吴国"),
(3,"诸葛亮","男","蜀国"),
(4,"貂蝉","女","东汉"),
(5,"赵子龙","男","蜀国"),
(6,"张飞","男","蜀国");

table:
+------+-----------+------+---------+
| id | name | sex | country |
+------+-----------+------+---------+
| 1 | 曹操 | 男 | 魏国 |
| 2 | 小乔 | 女 | 吴国 |
| 3 | 诸葛亮 | 男 | 蜀国 |
| 4 | 貂蝉 | 女 | 东汉 |
| 5 | 赵子龙 | 男 | 蜀国 |
| 6 | 张飞 | 男 | 蜀国 |
+------+-----------+------+---------+
1.查找所有蜀国人信息
select * from hero where country="蜀国"
2.查找女英雄的信息,只显示姓名,性别和国家
select name,sex,country from hero where sex="女"
3.把曹操的国籍改为蜀国
update hero set country="蜀国" where name="曹操";
4.把张飞的性别改为女,国籍改为泰国
update hero set sex="女",country="泰国" where name="张飞";
5.把id为2的记录的姓名改为司马懿,性别改为男,国家改为魏国
update hero set name="司马懿",sex="男",country="魏国" where id=2;
6.删除所有泰国人
delete from hero where country="泰国";
7.删除所有的英雄
delete from hero;
==============================================
2-MOSHOU.sanguo.txt
use MOSHOU;
create table sanguo(
id int,
name char(20),
gongji int,
fangyu tinyint unsigned,
sex enum("男","女"),
country varchar(20)
);
insert into sanguo values
(1,"诸葛亮",120,20,"男","蜀国"),
(2,"司马懿",119,25,"男","魏国"),
(3,"关6羽",188,60,"男","蜀国"),
(4,"赵云666",200,66,"男","魏国"),
(5,"8孙权",110,20,"男","吴国"),
(6,"貂蝉",666,10,"女","魏国"),
(7,null,1000,99,"男","蜀国"),
(8,"",1005,88,"女","蜀国");
table:
+------+-----------+--------+--------+------+---------+
| id | name | gongji | fangyu | sex | country |
+------+-----------+--------+--------+------+---------+
| 1 | 诸葛亮 | 120 | 20 | 男 | 蜀国 |
| 2 | 司马懿 | 119 | 25 | 男 | 魏国 |
| 3 | 关6羽 | 188 | 60 | 男 | 蜀国 |
| 4 | 赵云666 | 200 | 66 | 男 | 魏国 |
| 5 | 8孙权 | 100 | 60 | 男 | 吴国 |
| 6 | 貂蝉 | 666 | 10 | 女 | 魏国 |
| 7 | NULL | 1000 | 99 | 男 | 蜀国 |
| 8 | | 1005 | 88 | 女 | 蜀国 |
+------+-----------+--------+--------+------+---------+
===========================================================
1.找出攻击力高于150的英雄的名字和攻击力
select name,gongji from sanguo where gongji>150;
2.找出防御力不等于100的英雄信息
select * from sanguo where fangyu!=100;
3.找出攻击力大于200的蜀国英雄的名字和攻击力
select name,gongji from sanguo where country="蜀国" and gongji>200;
4.查找蜀国和魏国的英雄信息
select * from sanguo where country in ("蜀国","魏国");
5.将吴国英雄中攻击值为110的英雄的攻击值设置为100,防御值设置为60
update sanguo set gongji=100,fangyu=60 where country="吴国" and gongji=110;
6.查找攻击值在100-200之间的蜀国英雄信息
select * from sanguo where country="蜀国" and (gongji between 100 and 200);
7.查找除去蜀国和吴国的女英雄信息
select * from sanguo where country not in("蜀国","吴国") and sex="女";
8.查找(id为1,3,5的蜀国英雄和貂蝉) 的编号,姓名,国家
select id,name,country from sanguo where name="貂蝉" or (country="蜀国" and id in (1,3,5) );
9.查找姓名为null的蜀国男英雄信息
select * from sanguo where name is null and country="蜀国" and sex="男";
10.查找姓名为""的英雄的id,姓名,国家
select id,name,country from sanguo where name="";
11.将英雄的按防御值从低到高排序
select * from sanguo order by fangyu;
12.将蜀国英雄按攻击值从高到低排序
select * from sanguo where country="蜀国" order by gongji desc;
13.将魏蜀两国的男英雄中名字为三个字的英雄按防御值升序排列
select * from sanguo where
country in ("魏国","蜀国") and sex="男" and name like "___" order by fangyu;
14.查找防御值第2名到第4名的蜀国英雄记录
select * from sanguo where country="蜀国" order by fangyu desc limit 1,3;
15.查找攻击值前三名且名字不为空值的蜀国英雄的记录
select * from sanguo where country="蜀国" and name is not null order by gongji desc limit 3;
16.攻击力最强值是多少
select max(gongji) as best from sanguo; 用best取代max(gongji)在表格内显示字段名
17.统计id,name两个字段分别有多少条记录
select count(id),count(name) from sanguo; count不统计null,统计""
18.统计蜀国英雄中攻击值大于200的英雄数量
select count(*) as "蜀国攻击力>200的英雄数量" from sanguo where country="蜀国" and gongji>200;
19.统计sanguo表中一共有几个国家
select country from sanguo group by country;
select distinct country from sanguo; distinct的实现
select distinct name,country from sanguo; 这句把name,country当成整体,记录必须完全一样才能去重,distinct后面可以跟n个字段
20.计算所有国家的平均攻击力
select country,avg(gongji) from sanguo group by country;
21.查找所有国家中 英雄数量最多的 前2名国家名称及英雄数量
select country,count(*) as "numbers(英雄)" from sanguo group by country order by count(*) desc limit 2;
22.找出平均攻击力大于105的国家的前2名,显示国家名称和平均攻击力
select country,avg(gongji) as "平均攻击力" from sanguo group by country having avg(gongji)>105 order by avg(gongji) desc limit 2;
23.计算蜀国一共有多少个英雄
select count(distinct name) from sanguo where country="蜀国";
24.查询时显示所有英雄的攻击力 * 10
select name,gongji*10,country from sanguo;
25找出每个国家攻击力最高的英雄的名字和攻击值(子查询)
select name,gongji from sanguo where (country,gongji) in(select country,max(gongji) from sanguo group by country);
==================================================
示例3
table province
+----+--------+-----------+
| id | pid | pname |
+----+--------+-----------+
| 1 | 130000 | 河北省 |
| 2 | 140000 | 陕西省 |
| 3 | 150000 | 四川省 |
| 4 | 160000 | 广东省 |
| 5 | 170000 | 山东省 |
| 6 | 180000 | 湖北省 |
| 7 | 190000 | 河南省 |
| 8 | 200000 | 海南省 |
| 9 | 200001 | 云南省 |
| 10 | 200002 | 山西省 |
+----+--------+-----------+

table city
+----+--------+--------------+--------+
| id | cid | cname | cp_id |
+----+--------+--------------+--------+
| 1 | 131100 | 石家庄市 | 130000 |
| 2 | 131101 | 沧州市 | 130000 |
| 3 | 131102 | 廊坊市 | 130000 |
| 4 | 131103 | 西安市 | 140000 |
| 5 | 131104 | 成都市 | 150000 |
| 6 | 131105 | 重庆市 | 150000 |
| 7 | 131106 | 广州市 | 160000 |
| 8 | 131107 | 济南市 | 170000 |
| 9 | 131108 | 武汉市 | 180000 |
| 10 | 131109 | 郑州市 | 190000 |
| 11 | 131110 | 北京市 | 320000 |
| 12 | 131111 | 天津市 | 320000 |
| 13 | 131112 | 上海市 | 320000 |
| 14 | 131113 | 哈尔滨 | 320001 |
| 15 | 131114 | 雄安新区 | 320002 |
+----+--------+--------------+--------+

table county
+----+--------+--------------+--------+
| id | coid | coname | copid |
+----+--------+--------------+--------+
| 1 | 132100 | 正定县 | 131100 |
| 2 | 132102 | 浦东新区 | 131112 |
| 3 | 132103 | 武昌区 | 131108 |
| 4 | 132104 | 哈哈 | 131115 |
| 5 | 132105 | 安新县 | 131114 |
| 6 | 132106 | 容城县 | 131114 |
| 7 | 132107 | 雄县 | 131114 |
| 8 | 132108 | 嘎嘎 | 131115 |
+----+--------+--------------+--------+
3.1.内连接
1.从省表和市表中提取省名和市名一一对应
select province.pname,city.cname from province
inner join city
on province.pid = city.cp_id; #只显示完全匹配的值
2.从省市县表中提取省市县名一一对应
select province.pname,city.cname,county.coname from province
inner join city on province.pid = city.cp_id
inner join county on county.copid = city.cid; #连接的是前面生成的结果集
3.用左连接以省表为主显示省市详细信息
select province.pname,city.cname from province
left join city on province.pid = city.cp_id; #省表显示所有,匹配的市表有显示,未匹配显示null
4.用左连接以省表为主显示省市县详细信息
select province.pname,city.cname,county.coname from province
left join city on province.pid = city.cp_id
left join county on county.copid = city.cid;
5.显示省市县详细信息,要求县全部显示
select province.pname,city.cname,county.coname from province
left join city on province.pid = city.cp_id
right join county on county.copid = city.cid;
6.显示省市区详细信息,要求 市 全部显示
select province.pname,city.cname,county.coname from province
right join city on province.pid = city.cp_id
left join county on county.copid = city.cid;

mysql的示例及练习的更多相关文章

  1. JAVA使用jdbc连接MYSQL简单示例

    以下展示的为JAVA使用jdbc连接MYSQL简单示例: import java.sql.DriverManager; import java.sql.ResultSet; import java.s ...

  2. Mysql语句示例

    Mysql语句示例 最常用 sql 语句总结 前言 Mysql 是数据库开发使用的主要平台之一.sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句 ...

  3. MySQL安装示例数据库

    MySQL安装示例数据库 本文档演示如何下载及安装MySQL示例数据库sakila及employees数据库 1. 安装sakila数据库 1.1 下载sakila数据库 wget http://do ...

  4. mysql死锁示例

    MySQL有三种锁的级别:页级.表级.行级. MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking):BDB存储引擎采用的是页面锁(page-level locki ...

  5. 创建MySQL存储过程示例

    创建MySQL存储过程是学习MySQL数据库必须要掌握的知识,下文对创建MySQL存储过程作了详细的介绍,供您参考学习. AD:2013大数据全球技术峰会课程PPT下载 下文将教您如何创建MySQL存 ...

  6. mysql 视图示例

    基本操作 建立视图 CREATE VIEW view_test(qty,price,total) AS SELECT quantity,price,quantity*price FROM t; 多表视 ...

  7. SpringBoot+MyBatis+Mysql 详细示例

    SpringBoot与MyBatis整合,底层数据库为mysql的使用示例  项目下载链接:https://github.com/DFX339/bootdemo.git   新建maven项目,web ...

  8. mysql asyn 示例

    这篇文章摘自mysql asyn作者的博客,博客地址 开头有一个简单示例,然后是一个在play上的应用.例子我并没有跑过,但是仍能学到不少东西. object BasicExample { def m ...

  9. mysql 函数示例(转)

    MySQL函数大全及用法示例 1.字符串函数ascii(str)   返回字符串str的第一个字符的ascii值(str是空串时返回0)  mysql> select ascii('2');   ...

  10. MySQL操作示例

    """ MySQL综合练习作业 """ # 1.自行创建测试数据: # 创建数据库 """ create da ...

随机推荐

  1. Leedcode算法专题训练(字符串)

    4. 两个字符串包含的字符是否完全相同 242. Valid Anagram (Easy) Leetcode / 力扣 可以用 HashMap 来映射字符与出现次数,然后比较两个字符串出现的字符数量是 ...

  2. pandas(1):Pandas文件读取——read_excel()

    目录 一.函数原型 二.功能说明 三.常用参数说明 四.总结 一.函数原型 pd.read_excel(io, sheet_name=0, header=0, names=None, index_co ...

  3. 结合k8s和pipeline的流水线,并通过k8s接口完成镜像升级

    现在这家单位的CICD比较的混乱,然后突发奇想,想改造下,于是就用pipeline做了一个简单的流水线,下面是关于它的一些介绍 写一个简单的流水线 大概就是这么个流程简单来说就是:拉代码---> ...

  4. 嗝,我饱了——IDEA食用指南

    1 概述 IDEA全称IntelliJ IDEA,主要用于Java开发的IDE,代码自动提示,重构,JUnit,代码分析等的功能非常牛逼,这篇文章首先介绍目前为止IDEA最新版本的特性,然后从UI,常 ...

  5. SQL Server CDC配合Kafka Connect监听数据变化

    写在前面 好久没更新Blog了,从CRUD Boy转型大数据开发,拉宽了不少的知识面,从今年年初开始筹备.组建.招兵买马,到现在稳定开搞中,期间踏过无数的火坑,也许除了这篇还很写上三四篇. 进入主题, ...

  6. python进阶(15)多线程与多进程效率测试

    前言 在Python中,计算密集型任务适用于多进程,IO密集型任务适用于多线程   正常来讲,多线程要比多进程效率更高,因为进程间的切换需要的资源和开销更大,而线程相对更小,但是我们使用的Python ...

  7. Neo4j/cypher学习笔记与学习建议

    简介 本笔记的主要内容是 cypher 查询语言的编写与使用. 笔记主要整理自w3cschool上的neo4j教程以及Neo4j中文网所提供的cypher中文文档,此外还包括少量从其他个人博客与官方手 ...

  8. Pytest系列(30)- 使用 pytest-xdist 分布式插件,如何保证 scope=session 的 fixture 在多进程运行情况下仍然能只运行一次

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 背景 使用 pytest-xdis ...

  9. windows下使用tail -f 命令查看实时日志

    经常在linux后台进行日志分析的同学对tail -f 这个命令肯定不陌生了,监控实时系统日志简直不要太方便.但是作为一个自动化测试工程师,我们的代码实际上在本地跑就够了,不需要部署,但是window ...

  10. POJ2752KMP逆序处理

    题意:      给你一个串,问你都哪些位置即使前缀又是后缀. 思路:      可以用KMP来做,尝试过很多种方法,想把两个串接起来然后..失败,后来又想可以倒着匹配,就是把整个串倒过来..失败,说 ...