基本 sql 查询(指定列,条件查询,排序和分页查询)

This commit is contained in:
flykhan 2023-08-24 18:21:13 +08:00
parent db9e1dd070
commit b520c1de82
15 changed files with 98 additions and 0 deletions

3
day9/DQL/sch_1.sql Normal file
View File

@ -0,0 +1,3 @@
use studb;

4
day9/DQL/search1.sql Normal file
View File

@ -0,0 +1,4 @@
use studb;
-- 所有列的查询
select * from t_stu;

6
day9/DQL/search10.sql Normal file
View File

@ -0,0 +1,6 @@
use studb;
-- 查询2022年入职的所有员工
select *
from t_emp
where hire_date between '2022-01-01' and '2022-12-31';

7
day9/DQL/search11.sql Normal file
View File

@ -0,0 +1,7 @@
use studb;
-- 按学生的出生日期降序显示
select *
from t_stu
order by birthday desc;

10
day9/DQL/search12.sql Normal file
View File

@ -0,0 +1,10 @@
use studb;
-- 显示年龄最大的前两位学生
-- 出去 NULL
select *
from t_stu
where birthday is not null
-- 年龄越大,出生日期越前
order by birthday asc
limit 0,2;

12
day9/DQL/search13.sql Normal file
View File

@ -0,0 +1,12 @@
use studb;
-- 按1页显示2条数据的方式显示学生表的第2页数据
/*
limit offset, size offset是0开始的, 0
offset = (page-1)*size, page是页号, size是每一页显示的大小
*/
select *
from t_stu
limit 2,2;

4
day9/DQL/search2.sql Normal file
View File

@ -0,0 +1,4 @@
use studb;
-- 查询指定的列
select sid, name from t_stu;

5
day9/DQL/search3.sql Normal file
View File

@ -0,0 +1,5 @@
use studb;
-- 查询到的内容按指定的标题显示
select sid '学号', name as '姓名'
from t_stu;

4
day9/DQL/search4.sql Normal file
View File

@ -0,0 +1,4 @@
use studb;
select t_stu.sid,t_stu.sex
from t_stu;

5
day9/DQL/search5.sql Normal file
View File

@ -0,0 +1,5 @@
use studb;
-- 使用表的别名查询
select a.*
from t_stu a;

5
day9/DQL/search6.sql Normal file
View File

@ -0,0 +1,5 @@
use studb;
-- 查询字段是一个表达式
select sid, name, 100 as score
from t_stu;

10
day9/DQL/search7.sql Normal file
View File

@ -0,0 +1,10 @@
use studb;
-- 从多个表中查询数据时,如果存在相同的字段时,字段名前必须加表名
select *
from t_stu t1, t_stu t2;
where t1.sid > 2 and t2.sid > 3;
/*
*/

11
day9/DQL/search7_2.sql Normal file
View File

@ -0,0 +1,11 @@
use studb;
-- 从多个表中查询数据时,如果存在相同的字段时,字段名前必须加表名
select distinct t1.sid
from t_stu t1, t_stu t2;
where t1.sid > 2 and t2.sid > 3;
/*
7
*/

6
day9/DQL/search8.sql Normal file
View File

@ -0,0 +1,6 @@
use studb;
-- 查询员工表中手机号第7位是8的员工
select *
from t_emp
where tel like '______8%';

6
day9/DQL/search9.sql Normal file
View File

@ -0,0 +1,6 @@
use studb;
-- 查询disen和jack两位员工
select *
from t_emp
where name in ('disen', 'jack');