sql server 数据查询语句

数据操作:
1、插入语句:
insert into table_name values('value1','value2') 
2、删除语句:
delete from table_name where value1_name='value';
3、修改语句:
update table_name set value2_name='value' where value1_name=value1;
4、查询语句:
select * (结果集) from table_name where 条件;


sql数据查询:
1、with...as...:用于指定临时命名的结果集(cte表达式);
with person (name,age) as( select name age from tb_person where sex='女' )select * from person;
with agecount(age count) as (select age count(*)from tb_person where sex ='女' group by age) select * from agecount
2、select.....from子句;
select values from table_name
①、select...from子句的参数及说明:
all
distinct
top......
3、into子句:
into new Table;
select age name into student from person where age<20;(查询年龄小于20的人的姓名和年龄并将其插入到学生表中)
4、where子句:
and
or
not
like
not like
between and
is null
is not null
in
contains
all
some
any
freetext
①、逻辑运算符:and or not
and:将两个boolean条件拼接,只有全部返回真的时候才为真; 
select * from person where age>18 and sex='男';
or:将两个boolean条件拼接,一个返回真的时候就为真;
select * from person where sex='女'or age>60;
查询女性或者是年龄大于60的
not:对单个boolean值进行取反
select * from person where not sex='男' and not age<60;
查询性别不是男人且年龄不小于60岁的人
注意:逻辑运算符的运算顺序为not and or  必要的时候可以用()'括号'来操作;
②、where子句中允许出现的比较运算符:
= 测试两个表达式是否相等
<> :不等于
!= :不等于
>:大于
<:小于
!>:不大于
!<:不小于
<=:小于等于
>=:大于等于
  5、like关键字:
用来指定表达式是否与指定模式(通配符 % _ [^0~9],常规字符,)相匹配
select * from person where name like 张%;
查询姓张的所有人的信息
6、between关键字:
用between...and(大于等一第一个值,小于等于第二个值) 和not between...and(小于等一第一个值,大于等于第二个值)来指定范围条件 
select * from person where age not between 24 and 60;
7、is null/is not null关键字
在where子句中不能使用=来对空值进行判断,只能使用is not null /is null 来对空值进行判断;
select * from person where phone is not null

select * from person where phone is null;

8、in/not in 关键字:
在where子句中用in来指定字段的搜索条件:
select * from person where age in (22,23,24,28);查询年龄为22,23,24,28的所有人
select * from person where age not in (22,23,24,28);查询年龄不是22,23,24,28的人;
9、all some any关键字
all:查询字段和查询子集进行比较,和比较运算符(> = < != >= <= <> !> !<)一起使用<比所有的都大或者小……>
select * from person where age> all(select age from person where name in(’张三',‘李四’));查询比张三和李四年龄都大的人
some/any:查询字段和查询子集中的结果进行比较,和比较运算符一起使用,<比较其中的一个大于或者小于>;some 用在等号的时候
select * from person where age > any/some(select age from person where name in ('张三',‘李四’));查询年龄比张三或者李四大的人

10、exists关键字:
用()包裹
exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录,反之如果exists里的条件语句不能返回记录行,则当前loop到的这条记录被丢弃,exists的条件就像一个bool条件,当能返回结果集则为true,不能返回结果集则为 false;
select * from user where exists (select 1);
对user表的记录逐条取出,由于子条件中的select 1永远能返回记录行,那么user表的所有记录都将被加入结果集,所以与 select * from user;是一样的
select * from user where not exists (select 1);永远也查询不到任何信息;
11、group by 子句:
分组 按照一个或多个条件进行分组;
查询结果必须被包含在group by子句或者是聚集函数中
select name,count(*) from Employee group by name;统计查询同名的人数
select sex,name,age ,count(*) from person group by sex name age;统计查询有同名同龄同性别的人数
select sex count(*) from person where age>(select avg(age) from person) group by sex 统计查询年龄大于平均年龄的人并按照性别分组;
12、having子句:
对输出结果集进行限制输出,通常和group by(分组查询组合使用)
select age  from Employee group by Age having age>20;
统计查询年龄在20以上的男人和女人
select Sex, age ,count(*) as 人数  from Employee group by sex,age having age>20; 统计查询20岁以上的同龄 同性的人数;
13、order by子句:(top)
指定在select 返回列中按顺序排序
asc: 升序排列
desc:降序排列
select Sex, age ,count(*) as 人数  from Employee group by sex,age having age>20 order by age desc;
select Sex, age ,count(*) as 人数  from Employee group by sex,age having age>20 order by sex asc;
14、distinct关键字
从查询结果及中去除重复的记录
select distinct age from employee;
查询年龄并去除重复的
15、top关键字:
显示查询结果显示行数,
select top 5 * from employee ;查询前面5行数据
select top 10 from employee  order by id desc;查询后面10行数据;

16、union/union all合并多个字段:
将两个查询结果显示在一起(去除重复的)
select name,age from employee union select name,age from employer ;
union all 合并表:
不删除
select  name,age from  where sex='女' union all select name,age fromemployee where age>10;
union 中的 order by 子句:
select  name,age from Employee where sex='女' union all select name,age from employee where age>20 order by age;
注意采用union/union all 合并的时候不一定要数据类型完全相同,只要兼容就可以:
SELECT Sname,Sex FROM Student UNION ALL SELECT Cname,str(Credit) FROM Course 
合并不同列数的两个表:
SELECT Sname,Sex null FROM Student UNION ALL SELECT Cname,str(Credit) age FROM Course

多表合并:
SELECT Sname,Sex FROM Student UNION all SELECT Cno,Cname  FROM Course UNION all SELECT Sno,Cno FROM SC
17、子查询和嵌套查询:
①、子查询:嵌套在select insert update delete中的select语句;任何允许出现表达式的地方都可以用子查询但是子查询必须用()括号包裹起来
②、嵌套查询:将一个查询或多个查询嵌套在where 或者having 子句中;sql查询允许多层嵌套查询处理方式为先进行内查询再进行外查询;当子查询或者嵌套查询出现一列值的时候使用的是带in /not in的嵌套查询:
select * from employee where age>= some/any(select age from employee where name like '张%')

select *  from student where id in(select sid from sc );查询所有有成绩的学生;
select *  from student where id not in(select sid from sc );查询所有没有成绩的学生;
③、some any all嵌套:
select * from employee where age >any(select age from employee where id in(10,12)) 查询年龄比10号或者12号大的所有人
select * from employee where age >some(select age from employee where id in(10,12))查询年龄比10号或者12号大的所有人
select * from employee where age > all(select age from employee where id in(10,12))查询年龄比10号和12号都大的所有人
④、exists嵌套查询:
select * from student where exists(select null from sc where sc.sid=student.id and coures is not null);查询所有参加考试且有成绩的学生的信息;
18、联接查询:
①、内部联接:
使用比较运算符比较要联接列中的值的联接;会删除从其他联接表中没有匹配行的所有行所以可能会导致信息的丢失;
select column,count(*) from table_name1 inner join table_name2 on table_name1_column=table_name2_column where..... group by column  order by cloumn_name
select cname sname score from student inner join score on student.sno=score.sno inner join course on course.cno=score.cno;  

②、左外联接:
使用 left join on 包含左边表的所有行,和右边表中和左表关联的行 
select * from table left join table2 on table1.column=table2.column2
③、右外联接;
使用 right join on 包含右边表的所有行和左表的部分行
select * from table right join table2 on table1.column=table2.column2
④、完整外连接:
使用 full join 将左表和右表中的所有行都全输出,(union all 将结果集合并,列数=1/2/自定义 full join 列数等于1+2)
select * from table1 full join table2  on table1.column1=table2.clumn;
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值