//重建数据库 101, create database testdatabase;use database testdatabase; 102, create table tt1(id int, name varchar(20),age int,sex boolean); 103, show tables;desc tt1; //插入 104, insert into tt1 values(1,"zhang",25,0); 105, insert into tt1 values(2,"wang",25,1); 106, select * from tt1; 107, insert into tt1(id,name,age,sex) values(3,"li",28,1); 108, insert into tt1(id,name,sex,age) values(4,"sun",0,22); 109, insert into tt1(id,name,sex) values(5,"zhao",30,1); 110, insert into tt1(id,age,name) values(6,"he",47,0); 111, insert into tt1(id,age,name) values(7,"chen",22,1),(7,"zhang",22,1),(7,"xie",32,1); 112, select * from tt1; //修改 113, update tt1 set id=10,name="new",age=100,sex=0 where id=1; select * from tt1; 114, update tt1 set id=11,name="new" where id=2,age=25; select *from tt1; 115, updatett1 set id=12,sex=1 where id=7; select * from tt1; 116, update tt1 set sex=1 where id>3; 117, updatett1 set sex=0 where id<4; //删除 118, delete from tt1 where id=1;select * from tt1; 119, delete fromtt1 where id=12;select * from tt1; //查询 120, alter table tt1 add address varchar(30); 121, update tt1 set address="Beijing" where sex=1; 122, update tt1 set address="Shanghai" where sex=0; //简单查询 123, select id from tt1; 124, select id,name from tt1; 125, select id,name,address from tt1; //条件查询 126, select id,name,address from tt1 where address="Beijing"; 127, select * from tt1 where id in(2,3,4); 128, select * from tt1 where id not in(2,3,4); 129, select * from tt1 where id between 2 and 5; 130, select * from tt1 where id not between 2 and 5; 131, select * from tt1 where address like "beijing"; 132, select * from tt1 where address like "bei"; 133, select * from tt1 where address like "bei%"; 134, select * from tt1 where address not like "bei%"; 135, select * from tt1 where address is null; 136, select * from tt1 where address is not null; 137, select * from tt1 where age<20 and sex=1;138, select * from tt1 where sex=0 or age>30; //查询结果不重复 139, select distinct address from tt1;//查询结果排序 140, select * from tt1 order by age; 141, select * from tt1 order by age asc; 142, select * from tt1 order by age desc; //分组查询 143, select * from tt1 group by sex;//单独使用group by 只会选择每个分组的一条记录 //group by 与 group_concat结合使用 144, select group_concat(name),sex from tt1 group by sex; 145, select group_concat(name),group_concat(age),sex from tt1 group by sex; 146, select group_concat(name,age),sex from tt1 group by sex; //group by与集合函数结合使用 147, select sex,count(sex) from tt1 group by sex; 148, select sex,count(sex) from tt1 group by sex having sex>2;//having用于对分组后的结果加限制条件 149, select sex,count(sex) from tt1 group by sex having count(sex)>2; //with rollup 150, select sex,count(sex) from tt1 group by sex with rollup;//在显示记录的最后一行加一条,记录是上面所有记录的和,通常用于合计数量 //limit显示限制 151, select * from tt1; 152, select * from tt1 limit2; 153, select * from tt1 limit 3; 154, select * from tt1 limit 0,2; 155, select * from tt1 limit 1,2; //使用集合函数查询 //为了更好的理解本问题,新建一个表 156, create table grade(id int,name varchar(10),subject varchar(10),score int,sex boolean); 157, insert into grade values(1,"wang","math",100,1),(1,"wang","english",96,1),(1,"wang","physics",90,1); insert into grade values(2,"li","math",96,1),(2,"li","english",85,1),(2,"li","physics",99,1); insert into grade values(3,"sun","math",85,0),(3,"sun","english",98,0),(3,"sun","physics",80,0); 158, select * from grade; 159, select count(*) from grade; 160, select id,name,sum(score) from grade where id=1; 161, select id,name,sun(score) from grade group by id; 162, select id,name,sum(score) from grade group by id order by sum(score) desc; 163, select id,name,avg(score) from grade where id=2; 164, select id,name,avg(score),sum(score) from grade where id =3; 165, select id,name,avg(score) from grade group by id; 166, select subject,avg(score) from grade group by subject;* from 167, select subject,avg(score) from grade group by subject order by avg(score); 168, select name,max(score) from grade where subject="math"; 169, select name,max(score) from grade where subject="english"; //连接查询 //内连接 170, create table stu(id int,name varchar(10),ageint,sex boolean); 171, insert into stu values(1,"wang",25,1),(2,"li",23,1),(3,"sun",23,0),(4,"zhou",27,1),(5,"zhang",22,0); 172, select id,name,age,sex,score from stu,grade where stu.id=grade.id; 173, select stu.id,stu.name,stu.age,stu.sex,score from stu,grade where stu.id=grade.id; 174, select stu.id,stu.name,stu.age,stu.sex,score from stu,grade where stu.id=grade.id and score>90; //外连接 175, select stu.id,stu.name,stu.age,stu.sex,score from stu left join grade on stu.id=grade.id; 176, select stu.id,stu.name,stu.age,stu,sex,score from stu right join grade on stu.id=grade.id; 177, insert into grade values(4,"hu","math",99,1),(5,"liu","english",97,0); 178, select stu.id,stu.name,stu.age,stu.sex,subject,score from stu left join grade on stu.id=grade.id; 179, select stu.id,stu.name,stu.age,stu.sex,subject,score from stu right join grade on stu.id=grade.id; //子查询 180, select * from stu where idin (select *from grade); 181, select * from grade where id in (select * from stu); 182, create tablescholarship(level int,money int,score int); 183, insert into scholarship values(1,10000,90),(2,5000,85),(3,3000,80); 184, select id,name,score from grade where score>=(select score from scholarship where level=1); 185, select id,name,score from grade where score>=(select min(score) from scholarship);//exists子查询 186, select * from stu where exists (select name from grade where id=1); 187, select *from grade where score>any(select score from scholarship); 188, select * from grade where score>all(select score from scholarship); //合并查询结果 189, select name from stu union select name from grade;190, select name from stu union all select name from grade; //别名 191, select * from scholarship s where s.level=1; 192, select * from scholarship s where s.money=5000; 193, select s.name from stu s,grade g where s.name=g.name; 194, select subject as su,score as sc from grade; 195, select * from stu where name regexp "^w"; 196, select * from stu where name regexp "g$"; 197, select * from stu where name regexp "^w.g$"; 198, select * from stu where name regexp "^w..g$"; 199, select * from stu where name regexp "[aeo]"; 200, select * from stu where name regexp "[a-g]";

代码

//重建数据库 101, create database testdatabase;use database testdatabase; 102, create table tt1(id int, name varchar(20),age int,sex boolean); 103, show tables;desc tt1; //插入 104, insert into tt1 values(1,"zhang",25,0); 105, insert into tt1 values(2,"wang",25,1); 106, select * from tt1; 107, insert into tt1(id,name,age,sex) values(3,"li",28,1); 108, insert into tt1(id,name,sex,age) values(4,"sun",0,22); 109, insert into tt1(id,name,sex) values(5,"zhao",30,1); 110, insert into tt1(id,age,name) values(6,"he",47,0); 111, insert into tt1(id,age,name) values(7,"chen",22,1),(7,"zhang",22,1),(7,"xie",32,1); 112, select * from tt1; //修改 113, update tt1 set id=10,name="new",age=100,sex=0 where id=1; select * from tt1; 114, update tt1 set id=11,name="new" where id=2,age=25; select *from tt1; 115, updatett1 set id=12,sex=1 where id=7; select * from tt1; 116, update tt1 set sex=1 where id>3; 117, updatett1 set sex=0 where id<4; //删除 118, delete from tt1 where id=1;select * from tt1; 119, delete fromtt1 where id=12;select * from tt1; //查询 120, alter table tt1 add address varchar(30); 121, update tt1 set address="Beijing" where sex=1; 122, update tt1 set address="Shanghai" where sex=0; //简单查询 123, select id from tt1; 124, select id,name from tt1; 125, select id,name,address from tt1; //条件查询 126, select id,name,address from tt1 where address="Beijing"; 127, select * from tt1 where id in(2,3,4); 128, select * from tt1 where id not in(2,3,4); 129, select * from tt1 where id between 2 and 5; 130, select * from tt1 where id not between 2 and 5; 131, select * from tt1 where address like "beijing"; 132, select * from tt1 where address like "bei"; 133, select * from tt1 where address like "bei%"; 134, select * from tt1 where address not like "bei%"; 135, select * from tt1 where address is null; 136, select * from tt1 where address is not null; 137, select * from tt1 where age<20 and sex=1;138, select * from tt1 where sex=0 or age>30; //查询结果不重复 139, select distinct address from tt1;//查询结果排序 140, select * from tt1 order by age; 141, select * from tt1 order by age asc; 142, select * from tt1 order by age desc; //分组查询 143, select * from tt1 group by sex;//单独使用group by 只会选择每个分组的一条记录 //group by 与 group_concat结合使用 144, select group_concat(name),sex from tt1 group by sex; 145, select group_concat(name),group_concat(age),sex from tt1 group by sex; 146, select group_concat(name,age),sex from tt1 group by sex; //group by与集合函数结合使用 147, select sex,count(sex) from tt1 group by sex; 148, select sex,count(sex) from tt1 group by sex having sex>2;//having用于对分组后的结果加限制条件 149, select sex,count(sex) from tt1 group by sex having count(sex)>2; //with rollup 150, select sex,count(sex) from tt1 group by sex with rollup;//在显示记录的最后一行加一条,记录是上面所有记录的和,通常用于合计数量 //limit显示限制 151, select * from tt1; 152, select * from tt1 limit2; 153, select * from tt1 limit 3; 154, select * from tt1 limit 0,2; 155, select * from tt1 limit 1,2; //使用集合函数查询 //为了更好的理解本问题,新建一个表 156, create table grade(id int,name varchar(10),subject varchar(10),score int,sex boolean); 157, insert into grade values(1,"wang","math",100,1),(1,"wang","english",96,1),(1,"wang","physics",90,1); insert into grade values(2,"li","math",96,1),(2,"li","english",85,1),(2,"li","physics",99,1); insert into grade values(3,"sun","math",85,0),(3,"sun","english",98,0),(3,"sun","physics",80,0); 158, select * from grade; 159, select count(*) from grade; 160, select id,name,sum(score) from grade where id=1; 161, select id,name,sun(score) from grade group by id; 162, select id,name,sum(score) from grade group by id order by sum(score) desc; 163, select id,name,avg(score) from grade where id=2; 164, select id,name,avg(score),sum(score) from grade where id =3; 165, select id,name,avg(score) from grade group by id; 166, select subject,avg(score) from grade group by subject;* from 167, select subject,avg(score) from grade group by subject order by avg(score); 168, select name,max(score) from grade where subject="math"; 169, select name,max(score) from grade where subject="english"; //连接查询 //内连接 170, create table stu(id int,name varchar(10),ageint,sex boolean); 171, insert into stu values(1,"wang",25,1),(2,"li",23,1),(3,"sun",23,0),(4,"zhou",27,1),(5,"zhang",22,0); 172, select id,name,age,sex,score from stu,grade where stu.id=grade.id; 173, select stu.id,stu.name,stu.age,stu.sex,score from stu,grade where stu.id=grade.id; 174, select stu.id,stu.name,stu.age,stu.sex,score from stu,grade where stu.id=grade.id and score>90; //外连接 175, select stu.id,stu.name,stu.age,stu.sex,score from stu left join grade on stu.id=grade.id; 176, select stu.id,stu.name,stu.age,stu,sex,score from stu right join grade on stu.id=grade.id; 177, insert into grade values(4,"hu","math",99,1),(5,"liu","english",97,0); 178, select stu.id,stu.name,stu.age,stu.sex,subject,score from stu left join grade on stu.id=grade.id; 179, select stu.id,stu.name,stu.age,stu.sex,subject,score from stu right join grade on stu.id=grade.id; //子查询 180, select * from stu where idin (select *from grade); 181, select * from grade where id in (select * from stu); 182, create tablescholarship(level int,money int,score int); 183, insert into scholarship values(1,10000,90),(2,5000,85),(3,3000,80); 184, select id,name,score from grade where score>=(select score from scholarship where level=1); 185, select id,name,score from grade where score>=(select min(score) from scholarship);//exists子查询 186, select * from stu where exists (select name from grade where id=1); 187, select *from grade where score>any(select score from scholarship); 188, select * from grade where score>all(select score from scholarship); //合并查询结果 189, select name from stu union select name from grade;190, select name from stu union all select name from grade; //别名 191, select * from scholarship s where s.level=1; 192, select * from scholarship s where s.money=5000; 193, select s.name from stu s,grade g where s.name=g.name; 194, select subject as su,score as sc from grade; 195, select * from stu where name regexp "^w"; 196, select * from stu where name regexp "g$"; 197, select * from stu where name regexp "^w.g$"; 198, select * from stu where name regexp "^w..g$"; 199, select * from stu where name regexp "[aeo]"; 200, select * from stu where name regexp "[a-g]";

SQL语句200条(转)的更多相关文章

  1. MYSQL基础常见常用语句200条

    数据库 # 查看所有的数据库 SHOW DATABASES ; # 创建一个数据库 CREATE DATABASE k; # 删除一个数据库 DROP DATABASE k; # 使用这个数据库 US ...

  2. 数据库安装和基本sql语句

    数据库概念 文件作为数据进行存储,数据格式千差万别 将保存数据的地方统一起来 MYSQL--------->一款应用软件 用来帮你操作文件的 只要是基于网络通信,底层就是socket 服务端 - ...

  3. Spring 中jdbcTemplate 实现执行多条sql语句

    说一下Spring框架中使用jdbcTemplate实现多条sql语句的执行: 很多情况下我们需要处理一件事情的时候需要对多个表执行多个sql语句,比如淘宝下单时,我们确认付款时要对自己银行账户的表里 ...

  4. java执行多条SQL语句

    一次执行多条SQL的技术要点如下: DatabaseMetaData接口是描述有关数据库的整体综合信息,由于DatabaseMetaData是接口,所以没有构造方法,故不能使用new来创建Databa ...

  5. 由一条sql语句想到的子查询优化

    摘要:相信大家都使用过子查询,因为使用子查询可以一次性的完成很多逻辑上需要多个步骤才能完成的SQL操作,比较灵活,我也喜欢用,可最近因为一条包含子查询的select count(*)语句导致点开管理系 ...

  6. Oracle之SQL语句性能优化(34条优化方法)

    (1)选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处 ...

  7. Oracle SQL语句优化34条

    非常好用的SQL语句优化34条 1)选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 dri ...

  8. 一条Sql语句分组排序并且限制显示的数据条数

    如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...

  9. 不同数据库,查询前n条数据的SQL语句

    不同的数据库,支持的SQL语法略有不同,以下是不同数据库查询前n条数据的SQl语句 SQL Server(MSSQL) SELECT TOP n * FROM table_name ORACLE SE ...

随机推荐

  1. hihoCoder #1184 : 连通性二·边的双连通分量(边的双连通分量模板)

    #1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老 ...

  2. Python之Selenium的爬虫用法

    Selenium 2,又名 WebDriver,它的主要新功能是集成了 Selenium 1.0 以及 WebDriver(WebDriver 曾经是 Selenium 的竞争对手).也就是说 Sel ...

  3. 《HBase实战》学习笔记

    第二章  入门 HBase写路径: 增加新行和修改已有的行,内部机制是一样的. 写入的时候,会写到预写日志(WAL)和MemStore中. MenmStore是内存里的写入缓冲区.填满后,会将数据刷写 ...

  4. 【LOJ】#6391. 「THUPC2018」淘米神的树 / Tommy

    题解 一道非常神仙的计数题 如果只有一个点,就是非常简单的树型dp \(f_{u} = (siz_{u} - 1)! \prod_{v \in son_{u}} \frac{f_{v}}{siz_{v ...

  5. linux中shell,awk,sed截取字符串方法总结

    转自:http://www.cnblogs.com/kinga/p/5772566.html Shell 第一种: ${parameter%word} 最小限度从后面截掉word${parameter ...

  6. thinphp中volist嵌套循环时变量$i 被污染问题,key="k"

    默认是$i,但是嵌套循环是使用$i,默认的变量$i就会被污染.可以自定义设置变量key="k" k任意. 用 key="k" 代替默认的 $i 1 2 3 4 ...

  7. JAVAEE——宜立方商城12:购物车实现、订单确认页面展示

    1. 学习计划 第十二天: 1.购物车实现 2.订单确认页面展示 2. 购物车的实现 2.1. 功能分析 1.购物车是一个独立的表现层工程. 2.添加购物车不要求登录.可以指定购买商品的数量. 3.展 ...

  8. [ 转载 ] Java基础4--Java中的static关键字解析

    Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...

  9. 鸟哥的私房菜:Bash shell(四)-Bash shell的使用环境

    Bash shell(四)-Bash shell的使用环境   是否记得我们登入主机的时候,屏幕上头会有一些说明文字,告知我们的 Linux 版本啊什么的, 还有,登入的时候,我们还可以给予使用者一些 ...

  10. hdu 4462 第37届ACM/ICPC 杭州赛区 J题

    题意:有一块n*n的田,田上有一些点可以放置稻草人,再给出一些稻草人,每个稻草人有其覆盖的距离ri,距离为曼哈顿距离,求要覆盖到所有的格子最少需要放置几个稻草人 由于稻草人数量很少,所以状态压缩枚举, ...