sql语句复习

SQL各种语句复习

一、查询

select * from 表 where id=1

1. as更改列名

select name as ces from a where tea='nan'

说明:查询ces表中性别为nan的所有行,显示name列,并将name列改名为(姓名)显示

  • 例:select name, ‘测试’ as 职业 from man**

说明:查询表a,显示name列,并添加地址列,其列值都为’唐山’

2. 查询空

select * from 表 where name is null

3. 查询前6条数据

select top 6 name from a

说明:查询表a,显示列name的前6行,top为关键字

4. 排序

select name from a where chengji>=60 order by desc

说明:查询a表中chengji大于等于60的所有行,并按降序显示name列;默认为ASC升序

5. 模糊查询

select * from a where work like 'c%'

说明:查询显示表a中,name字段第一个字为赵的记录

6. 查询XX之间的数据

select * from a where nianling between 18 and 20

说明:查询显示表a中nianling在18到20之间的记录

7. 查询列表中的某一个内容

select worf from a where address in ('x','c','w')

说明:查询表a中address值为x或者c或者w的记录,显示worf字段

8. 分组后加条件

select name as 姓名,AVG(work) as 工作 from man group by studentID having count(score)>1

说明:接上面例子,显示分组后count(score)>1的行,由于where只能在没有分组时使用,分组后只能使用 having来限制条件

二、链表

1. 隐式链表

select a.name,b.chengji from a,b where a.name=b.name

说明:查询表a和表b中name字段相等的记录,并显示表a中的name字段和表b中的chengji字段

2. 左(外)链表

select s.name,c.courseID,c.score from strdents as s left outer join score as c on s.scode=c.strdentID

说明:在strdents表和score表中查询满足on条件的行,条件为score表的strdentID与strdents表中的 sconde相同

3. 右(外)链表

select s.name,c.courseID,c.score from strdents as s right outer join score as c on s.scode=c.strdentID

说明:在strdents表和score表中查询满足on条件的行,条件为strdents表中的sconde与score表的 strdentID相同

三、增加

insert into 表 values (‘A’,‘B’,‘C’)

1. 表数据迁移

insert into 新表 (values1,values2)select 旧表values from 旧表名

2. 以当前表数据创建新表并迁移数据

select 新表values into 新表名 from 旧表名

四、删除数据

delete from 表 where id=1

1. 删除表

truncate table 表

五、修改(更新)

update 表 set values=值 where ID=1

发布了8 篇原创文章 · 获赞 2 · 访问量 780

猜你喜欢

转载自blog.csdn.net/luojsjsjs/article/details/102976970