SQL 基本查询语句
--使用数据库
use date
go --创建表班级表
create table classInfo
(
classNo int primary key identity(1,1),--主键约束使用primary key identity
className varchar(10) not null--非空约束
)
go
--创建学员表
create table stuInfo --标识列约束使用identity
(
stuNo int primary key identity(1,1),--主键约束要求主键数据唯一并且不允许为空
stuname varchar(10)not null unique,--唯一约束使用unique关键字允许为空,但只能出现一个空值
age int check(age>10 and age<100),--检查约束使用check设置年龄
sex nchar(1)default'男',--默认约束
class int foreign key references classInfo(classNo)--外键约束用于两表之间建立关系,需要指定引用主表的那一列 )
go
--插入数据
insert into classInfo values('S2S147'),('S2S148')
insert into stuInfo values
('田七',18,'女',null),
('张三',38,'女',null),
('王五',20,'女',null),
('赵六',30,'男',null) --修改表中的数据
update stuInfo set class=1
--删除表中的数据
delete stuInfo where stuNo=1 --修改朱茂深的年龄和性别
update stuInfo set age=18,sex='男'where stuname='张三' --查询表中的数据
select * from stuInfo
select * from classInfo --查询性别为女的数据
select * from stuInfo where sex='女' --模糊查询使用like --查询'朱'字开头的名字
select * from stuInfo where stuname like '张%' --查询年龄18到20之间的数据
select * from stuInfo where age=18 or age=20
--使用in的效果一样
select * from stuInfo where age in(18,20)
select * from stuInfo where age between 18 and 20
--查询姓名年龄
select '姓名'=stuname,'年龄'=age from stuInfo where age between 18 and 20 --查询男生的人数(使用聚合函数)
select count(*) as 人数 from stuInfo where sex='男' --sum(求和)、avg(平均值)、max(最大值)、min(最小值)、count(计算总数) --查询男生女生分别多少人(分组)(group by 统计函数联合使用)
select 性别=sex, 人数=count(*) from stuInfo group by sex
--查询年龄18到20之间的个数大于等于2的数
select 性别=sex, 人数=count(*) from stuInfo
where age in (18,20) group by sex
having COUNT(*)>=2 --order by(作用是升降序 (ASC升从小到大排列)(Desc降)从大到小排列)
select * from stuInfo order by age asc
select * from stuInfo order by age desc --使用top查询前三条数据
--查询降序后的三条数据
select top 3 * from stuInfo order by age desc --查询出男生的 姓名 年龄 性别
select 姓名=stuname,年龄=age,性别=sex
from stuInfo,classInfo
where stuInfo.class=classInfo.classNo and sex='男' --内链接查询
--查询 姓名 年龄 性别 班级
select 姓名=stuname,年龄=age,性别=sex,班级=className
from stuInfo inner join classInfo
on stuInfo.class=classInfo.classNo
where sex='男' --左外连接查询使用(left join)
select stuname,age,sex,className
from stuInfo left join classInfo
on stuInfo.class=classInfo.classNo --右外连接查询 使用(right join)如果不满足条件的则返回null(空值)
select stuname,age,sex,classname
from stuInfo right join classInfo
on stuInfo.class=classInfo.classNo --全外连接查询使用(full outer join)outer可以不写
select stuname,age,sex,classname
from stuInfo full join classInfo
on stuInfo.class=classInfo.classNo --交叉链接查询(笛卡尔积)就是查询所指定要查询的值
select stuname,age,sex,classname
from stuInfo cross join classInfo --自连接查询查询张三所在班级的所有学员信息
select
姓名=s1.stuname ,
年龄=s1.age,
性别=s1.sex
from stuInfo s1,stuInfo s2
where s1.class=s2.class
and s2.stuname='张三' select
姓名=s1.stuname,
年龄=s1.age,
性别=s1.sex,
班级=c3 .className
from stuInfo s1, stuInfo s2 ,classInfo c3
where s1.class=s2.class
and s2.stuname='张三'
and s1.class=c3.classNo --子查询
select
姓名=stuname,
年龄=age,
性别=sex,
班级=class
from stuInfo
where class=(select class from stuInfo where stuname='张三') select
姓名=stuname,
年龄=age,
性别=sex,
班级=(select classname from classInfo where classNo=class)
from stuInfo
where class=
(select class from stuInfo where stuname='张三')
SQL 基本查询语句的更多相关文章
- SQL逻辑查询语句执行顺序 需要重新整理
一.SQL语句定义顺序 1 2 3 4 5 6 7 8 9 10 SELECT DISTINCT <select_list> FROM <left_table> <joi ...
- 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?
如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括:如何 ...
- python 3 mysql sql逻辑查询语句执行顺序
python 3 mysql sql逻辑查询语句执行顺序 一 .SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_t ...
- mysql第四篇--SQL逻辑查询语句执行顺序
mysql第四篇--SQL逻辑查询语句执行顺序 一.SQL语句定义顺序 SELECT DISTINCT <select_list> FROM <left_table> < ...
- SQL Server SQL高级查询语句小结(转)
--select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select disti ...
- 45、SQL逻辑查询语句执行顺序
一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOI ...
- sql的查询语句的总结
一:基本的查询sql 1:基本常用查询 select * from student; --select select all sex from student; --all 查询所有 select d ...
- NumberFormatException: Invalid int类型不匹配异常——使用SQL数据库查询语句select * from blacknumber order by _id desc limit ?,20;出现
异常:类型不匹配 05-06 08:12:38.151: E/AndroidRuntime(14904): java.lang.NumberFormatException: Invalid int: ...
- 第四篇:记录相关操作 SQL逻辑查询语句执行顺序
http://www.cnblogs.com/linhaifeng/articles/7372774.html 一 SELECT语句关键字的定义顺序 SELECT DISTINCT <selec ...
- SQL逻辑查询语句执行顺序
阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SELECT语句关键字的定义顺序 SELE ...
随机推荐
- servlet3.0文件上传与下载
描述:文件上传与下载是在JavaEE中常见的功能,实现文件上传与下载的方式有多种,其中文件上传的方式有: (1)commons-fileupload: (2)Servlet 3.0 实现文件上传 (3 ...
- CentOS7 策略路由配置
环境说明:Cloud1中的GE0/0/1.GE0/0/3.GE0/0/5接口,分别与Centos7中的eth1.eth2.eth3接口桥接到同一虚拟网卡,R1,R2,R3均配置一条静态默认路由指向Ce ...
- RabbitMq学习4-发布/订阅(Publish/Subscribe)
一.发布/订阅 分发一个消息给多个消费者(consumers).这种模式被称为“发布/订阅”. 为了描述这种模式,我们将会构建一个简单的日志系统.它包括两个程序——第一个程序负责发送日志消息,第二个程 ...
- Python 流程控制 超全解析(不可错过)
流程控制 程序执行结构流程 计算机程序在解决某个具体问题时,包括三种情形,即顺序执行所有的语句.选择执行部分的语句和循环执行部分语句,这正好对应着程序设计中的三种程序执行结构流程:顺序结构.选择结构和 ...
- 搜索专题: HDU1026Ignatius and the Princess I
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- NOI-LINUX
先把配置背过吧: (set-background-color "gray15")(set-foreground-color "gray")(global-lin ...
- HDU1688-POJ3463-Sightseeing(求次短路的条数)
题意 求出最短路和次短路的条数,当次短路比最短路长度小1时,输出条数之和,反之输出最短路条数. 题解 dis1[],cnt1[],dis2[],cnt2[] 分别表示最短路的长度和条数,次短路的长度 ...
- php手动实现ip2long和long2ip
php手动实现ip2long和long2ip /** * 测试 */ public function testipAction() { $ip = '10.58.101.175'; echo ip2l ...
- 前端开发HTML&css入门——伪类选择器和一些特殊的选择器
伪类和伪元素 有时候,你需要选择本身没有标签,但是仍然易于识别的网页部位,比如段落首行或鼠标滑过的连接.CSS为他们提供一些选择器:伪类和伪元素. 常用的一些伪类选择器: :link :visited ...
- 使用阿里ARouter路由实现组件化(模块化)开发流程
Android平台中对页面.服务提供路由功能的中间件,我的目标是 —— 简单且够用. 这是阿里对Arouter的定位,那么我们一起来梳理一下Arouter使用流程,和使用中我所遇到的一些问题! 先来看 ...