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;

 

 

  

  

posted @ 2024-07-17 17:44  这是啥!啥!啥  阅读(4)  评论(0编辑  收藏  举报