SQL 编写 排序时如何将NULL排在最后面

下面先直接排序看下效果


 
  1. select UserInfoID,User_No,User_Names

  2. from UserInfo

  3. order by User_NO asc

SQL 编写 排序时如何将NULL排在最后面

可以看到指定排序的列,其值为 null 的排在了最前面。

下面就是解决办法。


 
  1. select UserInfoID,User_No,User_Names

  2. from UserInfo

  3. order by case when User_NO is null then 1 else 0 end asc,User_NO asc

SQL 编写 排序时如何将NULL排在最后面

 

本人sql

select 
	upper(a.machine_sn) as "machineSn", 
	b.machine_type as "machineType", 
	c.project_name as "projectName", 
	get_affiliaction_name(a.class_id, a.department_id) as "affiliationName", 
	a.project_id as "projectId", 
	a.class_id as "classId", 
	a.department_id as "departmentId", 
	d.send_time as "sendTime", 
	a.usage_begin as "usageBegin", 
	a.usage_end as "usageEnd", 
	a.machine_status as "machineStatus", 
	a.notes as "notes", 
	a.modify_time as "modifyTime" 
  	
	from project_machine as a 
	
	left join machine_profile as b on a.machine_sn = b.machine_sn 
	left join project_profile as c on a.project_id = c.project_id 
	left join material_logistics as d on a.machine_sn = d.material_sn 
	and a.project_id = d.receive_project_id 
	and a.class_id = d.receive_class_id 
	and a.department_id = d.receive_department_id
	
	WHERE a.history_record = 1 
  order by case when a.modify_time is null then 0 else 1 end desc,a.modify_time desc

 

相关文章:

  • 2022-01-15
  • 2021-07-05
  • 2021-11-10
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
猜你喜欢
  • 2021-12-23
  • 2022-12-23
  • 2021-10-17
  • 2021-04-08
  • 2021-12-22
相关资源
相似解决方案