sql查询语句
基础查询
查询全部
select * from 表 where 条件;
查询指定列
select name, age from student
as别名:
select name as zName, age as zAge from student as zStudent
distinct去重:
select distinct name from student where age=18
逻辑运算:and且 or或 not非 sql优先and 在 or 除非加括号
regexp:正则表达式
select name from student where name regexp '李 | 三'
like模糊查询:
select name from student where name like '%张%'
范围:in 、not in 、between ... and ...
select age from student where age in (15, 16, 17, 18);
select age from student where age between 15 and 18;
order by 排序: asc:升 desc:降
select * from student order by age asc;
limit限制条数:
select * from student limit 2,4 (表中第三行,查询4条)
group by分组查
select age, count(*) from student group by age
高级查询
子查询(嵌套查询)
select * from stydent where age > ( select avg(age) from student)
排号
select row_name() over (order by score desc) 名次,name 姓名,score 成绩 from student;
聚合函数 最小值 最大值 求和 计数等;
select min(age) from student
连接查询 内连、外连、交叉
内连inner join: select * from 表1 inner join 表2 on 表1.字段 = 表2.字段;(两个表共有字段进行匹配,符合条件的合集拼接,on后面是连接条件,共有字段)
外连outer join: select * from 表1 left join 表2 on 表1.字段 = 表2.字段;
SELECT student.name, course.course_name FROM student LEFT JOIN course ON student.id = course.student_id;
SELECT student.name, course.course_name FROM student RIGHT JOIN course ON student.id = course.student_id;
交叉cross join: 交叉连接是指在两张或多张表之间没有任何连接条件的连接。简单来说,交叉连接可以让你查询所有可能的组合
SELECT t1.name, t2.name FROM tb1 t1 CROSS JOIN tb1 t2;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)