sql练习2

本文深入探讨了SQL查询的不同方面,包括删除记录、时间函数、连接操作、数据插入以及复杂查询的使用。通过示例展示了如何处理空值、时间差计算、最小值查找、多表连接和特定时间段的数据筛选。同时,文章还涵盖了日期格式转换和数据增长趋势的分析。
摘要由CSDN通过智能技术生成

delete 
from exam_record
where score is null
or timestampdiff(minute,start_time,submit_time)<5

order by start_time

limit 3

时间相减函数timestampdiff

Select a.device_id,a.university,a.gpa
from user_profile a
join (
    select university,min(gpa) as gpa 
    from user_profile
    group by university
) b
on a.university=b.university and a.gpa=b.gpa
order by a.university

 join的用法,自定义表,进行连接

select distinct user_info.uid,nick_name,achievement
from user_info,exam_record,practice_record
where nick_name like '牛客%号'
and achievement in (1200,2500)
and user_info.uid=exam_record.uid
and practice_record.uid=user_info.uid
and (month(start_time)='2021-09'
    or month(exam_record.submit_time)='2021-09'
    or month(practice_record.submit_time)='2021-09')

INSERT INTO exam_record_before_2021(uid, exam_id, start_time, submit_time, score)
SELECT uid, exam_id, start_time, submit_time, score
FROM exam_record
WHERE YEAR(submit_time) < '2021';

 插入表时可用select语句

insert into table (...)select ...from

select end.emp_no,(end.salary - start.salary) growth 
from (select emp_no,salary
      from salaries 
      where to_date = '9999-01-01') end 
      left join (select s.emp_no,salary 
                 from salaries s
                 , employees e 
                 where s.emp_no = e.emp_no
                 and s.from_date = e.hire_date) start
                 on start.emp_no = end.emp_no 
                 order by growth

 

select a.emp_no,b.emp_no manager_no,a.salary emp_salary, b.salary manager_salary
from (select dept_emp.emp_no,salaries.salary,dept_emp.dept_no
     from salaries,dept_emp
     where salaries.to_date='9999-01-01'
     and salaries.emp_no=dept_emp.emp_no) a,
     (select dept_manager.emp_no,salaries.salary,dept_manager.dept_no
     from salaries,dept_manager
     where 
     salaries.to_date='9999-01-01') b
     where b.dept_no=a.dept_no
     and b.salary<a.salary

select *
from student
where week(date_format('2020-04-21 00:00:00.000000','%Y%m%d'))=week(Sage)

date_format函数用来将字符串转化为date形式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值