数据库基本查询与高级查询

一、基本查询:

select * from Fruit --查所有
select Name,Source from Fruit --查特定列

select Ids ‘代号‘,Name ‘名称‘,Price ‘价格‘,Source ‘产地‘ from Fruit -- 修改列名

select * from Fruit where Ids=‘K006‘
select * from Fruit where Price=2.4 and Source=‘烟台‘ -- 查指定行按条件查

select * from Fruit where Price between 2.0 and 4.0 --查指定行按范围查

select * from Fruit where Numbers in (90,80,70)--查指定行,离散查

select distinct Numbers from Fruit --去重查询

select * from News

select * from News where title like ‘%户口‘ --模糊查询,查以户口结尾的
select * from News where title like ‘路飞%‘ --模糊查询,查以路飞开头的
select * from News where title like ‘%路飞%‘ --模糊查询,查以包含路飞的
select * from News where title like ‘%外币货_‘--模糊查询,查外币货之后只有一个字符的

select * from Fruit order by Numbers asc --按照Numbers列升序排,如果不加asc默认以升序排
select * from Fruit order by Numbers desc --按照Numbers列降序排
select * from Fruit order by Numbers,Price --先按照Numbers排,然后再按照Price排

select COUNT(*) from Fruit --返回Fruit表里面有多少条数据
select AVG(Numbers)from Fruit --返回某一列的平均值
select SUM(Numbers) from Fruit --返回某一列的所有数据和
select MAX(Numbers) from Fruit --返回某一列中的最大值
select MIN(Numbers) from Fruit --返回某一列中的最小值

select *,(Price*0.8) as ‘折后价格‘ from Fruit --加一列数据库中没有的列,这里是加了8折后的价格列

select Numbers, COUNT(*) from Fruit group by Numbers --根据某一列分组,求出该组内成员的个数
select Numbers, COUNT(*) from Fruit group by Numbers having COUNT(*)>1--根据某一列分组,求出该组内成员的个数,返回成员个数大于1的

二、高级查询:

--连接查询
select * from Info,Nation -- 形成笛卡尔积
select * from Info,Nation where Nation.Code=Info.Nation  

--join on 内连接
select * from Info join Nation on Info.Nation = Nation.Code

--查哪位学生的哪一门课考了多少分
select student.sname,course.cname,score.degree from student join score on score.sno=student.sno join course on course.cno = score.cno

--右连接,右边表必须显示全,如果在左边表没有与之对应的信息,则补空值
select * from Info right join Nation on Info.Nation=Nation.Code
--左连接,左边表必须显示全,如果在右边表没有与之对应的信息,则补空值
select * from Info left join Nation on Info.Nation=Nation.Code
--全连接,左右两边的表都显示完全
select * from Info full join Nation on Info.Nation=Nation.Code

--联合查询,对于查出的两个或多个结构相同的表联合显示
select Code,Name from Info
union
select InfoCode,Name from Family

--------子查询------------------
--子查询的结果当做父查询的条件
select * from Info
--无关子查询,子查询执行是独立的,和父查询是没有关系的(没有用到父查询的东西)
select * from Info where year(Birthday)=(
select YEAR(Birthday) from info where Code=‘p005‘)

--相关子查询
select * from teacher
--求计算机系和电子工程系不同职称的老师信息
select * from teacher t1 where depart=‘计算机系‘ and not exists(
select * from teacher t2 where depart=‘电子工程系‘ and t1.prof = t2.prof)
union
select * from teacher t1 where depart=‘电子工程系‘
and not exists(
select * from teacher t2 where depart=‘计算机系‘ and t1.prof = t2.prof
)

--查询除了每门课最高分之外的其他学生信息。
select * from score

select * from score where degree not in(select MAX(degree) from score group by cno)--错误

select * from score s1 where degree not in(
select MAX(degree) from score s2 group by cno having s1.cno = s2.cno
)

--select * from score where degree not in(86,75)

--分页
select * from Car

select top 5 * from Car -- 前5条数据,第一页
select top 5 * from Car where Code not in(
select top 5 Code from Car
) -- 第二页的数据

select top 5 * from Car where Code not in(
select top 10 Code from Car
) --第三页的数据

select top 5 * from Car where Code not in(
select top (5*2) Code from Car
) 

select ceiling(COUNT(*)/5) from Car --求总页数

select * from Car where 条件 limit 跳过几条数据,取几条数据 --mysql里面的分页
时间: 2024-08-13 09:08:03

数据库基本查询与高级查询的相关文章

数据库基础学习4--表格的 增 删 改 查(简单查询与高级查询)

一.增 C:create 增加,创建,向数据库里面添加数据. insert into Fruit values('K009','苹果',3.0,'高青',90,'') insert into Fruit(Ids,Name,Price,Source,Numbers) values('K010','苹果',3.0,'高青',90) 二.改 U:update修改,从数据库表里面修改数据. update Fruit set Source='烟台' where Ids='K001' 三.删 D:delet

MYSQL中的多类型查询及高级查询操作

离散查询select * from car where price=30 or price=40 or price=50 or price=60;select * from car where price in(30,40,50,60)取出数据select * from car where price not in(30,40,50,60)去掉数据 聚合函数(统计查询)select count(*) from carselect count(code) from car #取所有的数据条数sel

数据查询,简单查询及高级查询

查询所有列1.select * from info查特定列2.select code,name from info查出列后加别名,再查姓名3.select code as '代号',name as '姓名' from info条件查询,单条件查询4.select * from info where code='p001'两个条件,并且的关系5.select * from info where code='p001' and nation='n001'范围查询6.select * from car

[学习ES系列]-4.ElasticSearch基础交互-基础查询与高级查询

基础查询 POST http://127.0.0.1:9200/book/_search 1.简单查询 { "query":{ "match_all":{} } } 2.条件查询 { "query":{ "match":{ "title":"入门到精通" } }, "from":1, "size":5, "sort":{ &qu

EXISTS和 NOT EXISTS 子查询 (高级查询 二)

子查询:嵌入到另一个查询语句之中的查询语句 子查询注意事项: 1.子查询可以嵌套在sql语句中任何表达式出现的位置 2.只出现在子查询中没有出现在父查询中的表不能包含在输出列中 -----学生表DROP TABLE IF EXISTS student; CREATE TABLE `student`( `studentNo` INT(4) NOT NULL COMMENT '学号', `loginPwd` VARCHAR(20) NOT NULL COMMENT '密码', `studentNam

【2017-03-10】T-sql基础语句及条件,高级查询

一.T-sql基础语句 1.创建数据库:create database 数据库名  (不能中文,不能数字开头,不能符号开头) 2.删除数据库:drop database 数据库名 3.选择数据库:use 数据库名 4.创建表:create table 表名 ( 列名  数据类型, 列名  数据类型, 列名  数据类型 设置主键列:primary key 设置唯一列:unique 设置非空:not null 设置自增列:identity(1,1)   -从1开始计数,每次自增1 ) 5.删除表:d

Mysql 基础 高级查询

在西面内容中    car  和  nation   都表示 表名 1.无论 高级查询还是简单查询   都用  select.. from..语句   from  后面 加表名  可以使一张表也可以是多张表   表和表之间用逗号隔开 2. 简单查询和高级查询 不是 独立的   高级查询里面 同样可以用到 简单查询   3.简单查询与复杂查询的联系: 简单查询里面 后面的条件 未知时 需要用另一个 查询来代替  这样就变成了高级查询 4.链接查询  和  联合查询的区别:1. 链接查询 连接两张以

Winform开发框架之通用高级查询模块--SNF快速开发平台3.3-Spring.Net.Framework

最近项目确实忙,但也是一直忙于有关项目和框架技术的事情,也一直致力于改善我的WInform开发框架.使得自己及客户使用起来更加方便,更加友好,更加高效. 在很多程序模块中都很常见,也是给客户扩展查询的一个很好的补充,由于我一直希望我的Winform开发框架能够精益求精,所以做了这个模块,希望对今后我自己所有的项目以及框架本身,都能高效的使用. 1.通用高级查询模块的用途及介绍 既然称之为通用查询模块,那么他就不能与具体的表字段有耦合关系,但是要实现具体的查询,必须通过某种方式进行属性传递,实现更

数据库——基础(数据库操作,表格操作)——增加高级查询

笔记 LAMP:Linx(操作系统) A(阿帕奇)——网页的应用程序 M(Mysql):体积小,应用简单 P(PHP) 第一步:搭建网页环境——A\M\P WAMP:用WAMP搭建环境 DW:更好的显示 数据库的基本操作: 数据库——表结构——字段(列) 每一行数据成为一条数据(记录) 特点:关系型数据库,有严格的规范 1.必须有主键:能够唯一标识一条数据的字段 2 T-SQL:通用的数据库操作语句 自增长列code(主键列) ;连接键表 最后一个字段不加 ,#注释 创建表:create tab