SQL 常用语句

来源:互联网 发布:webpack.prod.js 编辑:程序博客网 时间:2024/06/10 18:13

create:insert [into]  table[(column1, column2...)]values(value1, value2...)[,(第二条值),(第三条值)...]

update:updatetableset column=value, c2=v2...  [一般后跟 条件 where,不跟,则更新所有]

delete:deletefrom table [一般后跟 条伯where,不跟,则删除所有]


聚合函数

avg  平均值

sum 求和

count 求满足条件的列的数量, 一般跟group by 一起使用

max 最大值

min 最小值


retrieve:

select  distinct *,count(name)as name_countfrom course [as] c       //distinct 去重,  as别名

where numweek = 1    //where 条件

and sid in (

    select id from sectionwhere hour > 6 or (hour=6 and minute > 10)

   )

group by  name   //分组

having id !=2        //分组group by  的条件

order by id desc[/asc]     //排序


表连接 (A为左表 B为右表)

左外连接  select * from A [as] a left [outer] join B [as] b on a.id=b.aid    

    以左表为准 通常是 左表全显示,右表显示符合on条件的,右表记录如果找不到,则为null

右外连接  select * from A [as] a right [outer] join B [as] b on a.id=b.aid    

    以右表为准 

内连接 select * from A [as] a inner [outer] join B [as] b on a.id=b.aid 

   符合条件  左右表 才显示

   内连接查询结果与这样的语句结果一样:select *from course a, section b where a.sid=b.id

0 0