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
自连接
自然连接 排除重复
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术