SQL 編寫 排序時如何將NULL排在最后面


下面先直接排序看下效果


 
  1. select UserInfoID,User_No,User_Names

  2. from UserInfo

  3. order by User_NO asc

可以看到指定排序的列,其值為 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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM