SQL必知必会(一)
select a from table
select a,b from table
select * from table
select a from table order by a
select a,b,c from table order by b,c 先按b后按c
select a,b,c from table order by 2,3 先按第2列后按第3列
select a from table order by a desc 降序 desc descending - asc ascending
select a from table where a=1
between(和and连用) / is null/ <>/…常见其他操作符
select a from table where a=1 and b=2
select a from table where a=1 or b=2
select a from table where( a=1 or a=2 )and b=2
优先级 and>or
select a from table where a in (1,2)
select a from table where not a=1
select a from table where a like '1%'
select a from table where a like '1_'
select a from table where a like '[JM]%'(SQLserver支持)
select a from table where a like '[^JM]%'
select a from table where not a like '[JM]%'
计算
拼接
select a + '('+bbbb+')' from table (SQLserver)
select a || '(' || bbbb || ')' from table (Oracle)
select concat(a ,'(',bbbb,')')(MySQL)
select rtrim(a)+ '('+rtrim(bbbb)+')' from table
rtrim去左空格 ltrim去右空格 trim去两边空格
select rtrim(a)+ '('+rtrim(bbbb)+')' as c from table
别名如果是字符串要加单引号
select a,b,c from table where a =1
select a,b,c,b*c as d from table where a=1
串 数据类型转换 取当前日期
MySQL substring convert curdate
SQLserver substring convert getdate
Oracle substr 多个 sysdate
left
length
lower
ltrim
right
soundex字符串转语音匹配发音
upper
rtrim
datepart(SQLserver) year(MySQL)to_date/to_num(to_char)(Oracle DD-MMM-YYYY)
abs绝对值
sin
cos
tan
exp指数
pi圆周率
sqrt平方根
avg 忽略null
count 指定列忽略空 *不忽略
max 部分可文本 忽略null
min 忽略null
sum 忽略null
all默认 distinct不同值 指定列名用于count不可count (*)
group by 有聚集函数必须用
having
子查询 灵活但是少用 要使用完全限定列名
二义性 使用完全限定列名
笛卡尔积 m*n行
等值连接 inner join ... on
as 别名 Oracle别名 Customer c
自连接
自然连接 排除重复