查询

1.简单查询

select * from info(表名)   --查所有数据

select  code(列名),name(列名)  from 表名        --查指定列的数据

select  code(列名) as ‘代号’,name(列名) as ‘姓名’ from info (改变列名)  --给列指定别名

2.条件查询

select * from info(表名) where code(条件)=‘p001’

select * from info(表名) where sex(条件)=‘true’,and nation=‘n001’   --多条件并的关系

select * from info(表名) where sex(条件)=‘true’,or nation=‘n001’    --多条件或的关系

3.范围查询

select * from 表名 where 范围

select * from car where price>40 and price<50

select * from car where price between 40 and 50

4.离散查询  

select * from car(表名) where code(列名) in(‘c001’,‘c005’,‘c010’,‘c015’)

select * from car where code not in(‘c001’,‘c005’,‘c010’,‘c015’)

5.模糊查询

select * from  car(表名) where name(列名) like ‘%宝马(关键字)%’   --查包含宝马的

select * from  car where name like ‘宝马%’    --查以宝马开头的

select * from  car where name like ‘%宝马     --查以宝马结尾的’

select * from  car where name like ‘宝马’      --查等与宝马的

select * from  car where name like‘—E%’   --查第三个字符是E的

%代表是任意多个字符

-代表是一个字符

6.排序查询

select *from car(表名) order by price(列名) asc  --以价格升序排列

select *from car(表名) order by price desc  –以价格降序排列

select *from car order by 0il desc, price asc  --以两个字段排序,前面的是主条件后面的是次要条件

7.分页查询

select  top 5 * from car

select  top 5 * from car where code not in (select  top 5 code from car)

当前页:page  = 2  row= 10

select top row *from car where code not in (select top (page-1) * row code from car)

8.去重查询

select distinct  brand from car

9.分组查询

select Brand from car group  by  Brand  having count(*)>2

10.聚合函数(统计查询)

select  count(*)查询完以后到底有多少数据 from car –查询所有数据条数

select  count(code)  from car    --查询所有数据条数

select  sum(price(列名)) from car(表名) 求和

select  sum(price) from car 求和

select  sum(price) from car 求和

select  sum(price) from car 求和

高级查询

1.连接查询

select * from Info,Nation --形成笛卡尔积

select * from Info,Nation where Info.Nation = Nation.Code

select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code

select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式

2.联合查询

select Code,Name from Info
union
select Code,Name from Nation

3.子查询

一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询查询的结果作为父查询的条件。

--查询民族为汉族的所有人员信息
select * from Info where Nation = (select Code from Nation where Name = '汉族')

(1)无关子查询

子查询可以单独执行,子查询和父查询没有一定的关系

--查询系列是宝马5系的所有汽车信息
select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = '宝马5系')

(2)相关子查询

--查找油耗低于该系列平均油耗的汽车

select * from Car where Oil<(该系列的平均油耗)
select avg(Oil) from Car where Brand = (该系列)

select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)

数据库SQL 查询的更多相关文章

  1. WordPress 常用数据库SQL查询语句大全

    在使用WordPress的过程中,我们少不了要对数据库进行修改操作,比如,更换域名.修改附件目录.批量修改文章内容等等.这个时候,使用SQL查询语句可以大大简化我们的工作量. 关于如何操作SQL查询语 ...

  2. [数据库] SQL查询语句表行列转换及一行数据转换成两列

    原文来自:http://blog.csdn.net/Eastmount/article/details/50559008 本文主要讲述了SQL查询语句表之间的行列转换,同时也包括如何将一行数据转换成两 ...

  3. MySql数据库 sql查询增加序号的伪列

    在查询数据库的时候,我们有时候需要对查询出来的数据加上序列,1,2,3,……n 例如:我们根据表的某个字段排序后,要对这些数据加上序列,这个时候序号常常不是我们建表时设置好的自增的主键id,怎么办呢? ...

  4. Yii2 数据库sql查询

    Yii2.0 对数据库 查询的一些简单的操作 User::find()->all(); //返回所有数据: User::findOne($id); //返回 主键 id=1 的一条数据(举个例子 ...

  5. 数据库 | SQL查询&LIMIT的用法

    body{ text-align:left; width:80%; margin:10px 100px; } 前言 select top n 形式的语句可以获取查询的前几个记录,但是 mysql没有此 ...

  6. 数据库SQL查询作业

    --设有三个关系 --S(S#,SNAME,AGE,SEX) --SC(S#,C#,GRADE) --C(C#,CNAME,TEACHER) --(1)检索LIU老师所授课程的课程号.课程名 sele ...

  7. SQL Server 数据库子查询基本语法

    一.SQL子查询语句 1.单行子查询        select ename,deptno,sal        from emp        where deptno=(select deptno ...

  8. SQL入门经典(二) 之数据库基本查询、添加、更新和删除

    使用SQL查询: SQL查询基本语法: SELECT [ALL|DISTINCT]  [TOP (<expression>) [PERCENT] [WITH TIES] ] <col ...

  9. SQL查询数据库是否存在

    在实际工作中会遇到通过SQL查询数据库是否存在的情况,下面一些语句可以提供一些帮助,本文的语句是在SQL08R2中测试的 1,查询当前数据库服务器所有数据库 select *  From master ...

随机推荐

  1. bpel 之伙伴

    一.伙伴链接类型(Partner Link Types) 1.交互过程 伙伴之间的交互过程共分为两种典型情况: 流程调用伙伴后同步等待返回结果.这种情况通常是伙伴能很快返回结果,流程不需要等待很长时间 ...

  2. jquery_easyui的使用

    一.引入jquery,jquery_easyui,jquery_easyui css,图标css,本地语言 二.通过学习jquery_easyui 手册,用简单的js代码来实现(按钮.表单.表格.弹出 ...

  3. PHP正则表达式;数组:for()遍历、 foreach ()遍历、each()list()组合遍历;指针遍历

    正则表达式:    1.定界符号        任何字符,一般用  //    2. 模式修正符i        写在定界符外面后面,可不区分大小写    3.preg_replace($reg,&q ...

  4. eclipse dbviewer,eclipse java8

    进入/home/xxx(用户名)/.local/share/applications,看是否有eclipse和深度音乐desktop配置文件,为eclipse.desktop配置图标, 那现在终端输入 ...

  5. Hue协作框架

    http://archive.cloudera.com/cdh5/cdh/5/hue-3.7.0-cdh5.3.6/manual.html 一:框架 1.支持的框架 ->job ->yar ...

  6. Qt 之 自定义按钮 在鼠标 悬浮、按下、松开后的效果(全部通过QSS实现)

    http://blog.csdn.net/goforwardtostep/article/details/53464925

  7. css3 loading效果

    file:///E:/zhangqiangWork/2014/SPDbank/index.html 参考该网站 http://tobiasahlin.com/spinkit/ 查看源代码把里面的dom ...

  8. There has been an error processing your request magento

    如果使用magento的过程中,出现以下页面: 说明出现了错误,但是亲,不用紧张,请根据"Error record number:xxxxxxxxx"的数字在网站根目录下的var/ ...

  9. Openmpi 编译安装+集群配置 + Ubuntu14.04 + SSH无密码连接 + NFS共享文件系统

    来源 http://www.open-mpi.org/ 网络连接 SSH连接,保证各台机器之间可以无密码登陆,此处不展开 hosts文件如下 #/etc/hosts 192.168.0.190 mas ...

  10. appium testcase2

    自己跑的两个case都在盘里,可以直接解压后放到workspace,加载工程就能跑,前提是你的环境没有问题 http://pan.baidu.com/s/1bnHCyn1 eclipse-File-i ...