您的位置:首页 > 数据库

sql常用

2015-11-16 19:54 411 查看
create:insert [into]  table[(column1, column2...)]values(value1, value2...)[,(第二条值),(第三条值)...]
update:updatetable set column=value, c2=v2...  [一般后跟 条件 where,不跟,则更新所有]
delete:deletefrom table [一般后跟 条伯where,不跟,则删除所有]

聚合函数
avg  平均值
sum 求和
count 求满足条件的列的数量, 一般跟group by 一起使用
max 最大值
min 最小值

retrieve:
select  distinct *,count(name)as name_count from 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: