Sql语句之select 5种查询
select 5种子句:注意顺序
where / group by /having / order by / limit /
清空表中的数据:truncate 表名;
导入表结构(不含数据): create table 表2 like 表1;
删除表:drop table 表名;
导入数据:insert into g2 select * from stu order by name, fenshu desc;
//从临时表中查询=========子查询
select * from (select * from stu order by name, fenshu desc) as tmp group by name;
where 表达式 把表达式放在行中,看表达式是否为真;
列 当成 “变量” 来理解 可以运算
查询结果 当成“临时表” 来理解
子查询:3种:
where型子查询:把内层的查询结果作为外层子查询的条件
select * from stu where fenshu=(select max(fenshu) from stu);
select * from stu where fenshu in (select max(fenshu) from stu group by name);
from 型子查询:把内层的查询结果当成临时表供外层继续查询:
select * from (select * from stu order by name, fenshu desc) as tmp group by name; //必须有as tmp做别名,否则报错;
exists型子查询:(难点)
把外层的查询结果拿到内层,看内层的查询是否成立;
select * from stu where exists (select * from goods where goods.cat_id = category.cat_id);
select 查询5子句之order by:
根据字段进行排序而已:
若要倒序排列则用“desc”来声明一下即可
显示声明升序排列用“asc”来声明;
select name, fenshu from stu order by fenshu, name desc;
limit关键字;起到限制条目作用;
limit [offset],N
offset:偏移量, 默认是0;
N:取出的条目
取第3行之后的4行;
select * from stu order by fenshu desc limit 3, 4;
select 查询5子句之having查询:(用于在缓冲区的查询,而不能在表中(即mysql的文件)查询)
select good_id, goods_name, market_price - shop_price as sheng from goods where
market_price > 200;
select good_id, goods_name, market_price -shop_price as sheng from goods having
sheng > 200;
select good_id, goods_name, market_price -shop_price as sheng from goods where cat_id = 3 having sheng >200;
select cat_id.sum(shop*goods_number) as huokuan from group by cat_id;
select cat_id.sum(shop*goods_number) as huokuan from group by cat_id having huokuan > 20000;
#每个人的平均分
select name, avg(scores) from stu group by name;
#每个人的挂科情况;
select name.scores < 60 from stu;
#每个人的挂科科目数目:
select name, sum(score < 60) from stu group by name;
select name, sum(score<60), avg(scores) as pj from stu group by name;
Sql语句之select 5种查询的更多相关文章
- oracle SQL语句练习MERGE、模糊查询、排序、
Oracle支持的SQL指令可分为数据操作语言语句.数据定义语言语句.事务控制语句.会话控制语句等几种类型:1.数据操作语言语句数据操作语言语句(Data manipulation language, ...
- SQL点滴10—使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比
原文:SQL点滴10-使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比 今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章 ...
- ASPNETCOREAPI 跨域处理 SQL 语句拼接 多条件分页查询 ASPNET CORE 核心 通过依赖注入(注入服务)
ASPNETCOREAPI 跨域处理 AspNetCoreApi 跨域处理 如果咱们有处理过MV5 跨域问题这个问题也不大. (1)为什么会出现跨域问题: 浏览器安全限制了前端脚本跨站点的访问资源, ...
- python快速导出sql语句(mssql)的查询结果到Excel,解决SSMS无法加载大字段的问题
遇到一个尴尬的问题,SSMS的GridView对于大字段的(varchar(max),text之类的),支持不太友好的,超过8000个长度之外的字符,SSMS的表格是显示不出来的(当然也就看不到了), ...
- [置顶] VB6基本数据库应用(三):连接数据库与SQL语句的Select语句初步
同系列的第三篇,上一篇在:http://blog.csdn.net/jiluoxingren/article/details/9455721 连接数据库与SQL语句的Select语句初步 ”前文再续, ...
- sql语句编写 有时候一个子查询可以拆分成多个子查询
sql语句编写 有时候一个子查询可以拆分成多个子查询
- LINQ to SQL语句(1)Select查询的九种形式
目录 说明 简单形式 匿名类型形式 条件形式 指定类型形式 筛选形式 Shaped形式 嵌套形式 本地调用方法形式 Distinct形式 说明 与SQL命令中的select作用相似但位置不同,查询表达 ...
- 使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比
今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章,自己添加了一些内容,做了简单的总结,这个语句还是第一次见到,学习了.我从简单到复杂地写, ...
- LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg (转)
Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...
随机推荐
- MySql连接异常解决
这两天遇到一个mysql连接的问题,找人弄了好几天也没弄好,先看一下报错信息: ============================================================ ...
- 关于scala和java 在maven项目中混编的问题
1.需要添加scala 相关maven配置: <properties> <scala.version>2.10.1</scala.version> <slf4 ...
- html5 canvas 运行起来绝对让你震撼!
从一个大神那看到的,拷贝过来跟大家分享下! html <canvas></canvas> *{margin:0;padding:0;}body{background:#222; ...
- 类 BufferedReader
以前学习的时候也没有太在意,在项目中使用到了才发现呵呵 1.读取一个txt文件,方法很多种我使用了字符流来读取(为了方便) FileReader fr = new FileReader("f ...
- 安装AppManager
http://www.searchvirtual.com.cn/showcontent_66884.htm
- flume 日志采集工具
Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并 ...
- java单链表代码实现
用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...
- rdo(remote data objects) repo openstack icehouse
problem making ssl connection Error: Cannot retrieve repository metadata (repomd.xml) for repository ...
- ubuntu安装XHProf
1. 安装XHProf wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2 s ...
- Problem A: 走迷宫问题
Problem A: 走迷宫问题Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 9 Solved: 3[Submit][Status][Web Board] ...