請教一SQL語句(UNION),請路過的朋友幫我看看(300分有答案嗎?) ( 积分: 50 )

  • 主题发起人 主题发起人 ljj_cxr
  • 开始时间 开始时间
L

ljj_cxr

Unregistered / Unconfirmed
GUEST, unregistred user!
有一表<br>aa&nbsp;&nbsp;bb&nbsp;&nbsp;cc<br>a1&nbsp;&nbsp;b1&nbsp;&nbsp;空<br>a2&nbsp;&nbsp;b2&nbsp;&nbsp;2007-01-01<br>a3&nbsp;&nbsp;b3&nbsp;&nbsp;2007-11-30<br><br>要求結果:按日期進行排序(升序),為空的排在最後。<br>結果:<br>aa&nbsp;&nbsp;bb&nbsp;&nbsp;cc<br>a2&nbsp;&nbsp;b2&nbsp;&nbsp;2007-01-01<br>a3&nbsp;&nbsp;b3&nbsp;&nbsp;2007-11-30<br>a1&nbsp;&nbsp;b1&nbsp;&nbsp;空
 
select&nbsp;*&nbsp;from&nbsp;Table1&nbsp;where&nbsp;not&nbsp;(cc&nbsp;is&nbsp;null)&nbsp;order&nbsp;by&nbsp;cc<br>union&nbsp;all<br>select&nbsp;*&nbsp;from&nbsp;Table1&nbsp;where&nbsp;cc&nbsp;is&nbsp;null
 
謝謝你的回復,已用過此方法,<br>結果沒有變動,<br>(是對Excel表格操作)剛才沒有寫清楚,不好意思。還有其它方法嗎?
 
各位朋友,有解決辦法嗎?<br>不勝感謝
 
在線等待呀。。<br>TO:Corn3朋友<br>你的方法在MSSQL中也為能通過,需將ORDER&nbsp;BY&nbsp;移到最後,<br>能否幫我測試一下
 
看來是不能用一條語句搞定啦。哎。。。
 
終於解決:<br>方法:<br>select&nbsp;&nbsp;*&nbsp;from&nbsp;(select&nbsp;&nbsp;top&nbsp;100000&nbsp;*&nbsp;from&nbsp;titles&nbsp;where&nbsp;advance&nbsp;is&nbsp;not&nbsp;null&nbsp;&nbsp;order&nbsp;by&nbsp;advance)&nbsp;aa<br>union&nbsp;all<br>select&nbsp;*&nbsp;from&nbsp;(select&nbsp;top&nbsp;100000&nbsp;*&nbsp;from&nbsp;titles&nbsp;where&nbsp;advance&nbsp;is&nbsp;null)&nbsp;bb
 
order&nbsp;by&nbsp;advance&nbsp;desc&nbsp;这样不行吗?
 
select&nbsp;*&nbsp;from&nbsp;Table&nbsp;order&nbsp;by&nbsp;isnull(cc,Cast('2099-01-01'&nbsp;as&nbsp;datetime))
 
晕!!还有这样的答案?<br>select&nbsp;&nbsp;*&nbsp;from&nbsp;(select&nbsp;&nbsp;top&nbsp;100000&nbsp;*&nbsp;from&nbsp;titles&nbsp;where&nbsp;advance&nbsp;is&nbsp;not&nbsp;null&nbsp;&nbsp;order&nbsp;by&nbsp;advance)&nbsp;aa<br>union&nbsp;all<br>select&nbsp;*&nbsp;from&nbsp;(select&nbsp;top&nbsp;100000&nbsp;*&nbsp;from&nbsp;titles&nbsp;where&nbsp;advance&nbsp;is&nbsp;null)&nbsp;bb&nbsp;&nbsp;<br>与以下没什么不成,只是没以下语句效率高:<br>select&nbsp;*&nbsp;from&nbsp;Table1&nbsp;where&nbsp;not&nbsp;(cc&nbsp;is&nbsp;null)&nbsp;order&nbsp;by&nbsp;cc<br>union&nbsp;all<br>select&nbsp;*&nbsp;from&nbsp;Table1&nbsp;where&nbsp;cc&nbsp;is&nbsp;null
 
不是很难啊<br>变通一,把cc为null的先update&nbsp;为&nbsp;now()&nbsp;+&nbsp;1(反正就是最大的日期加1即可)&nbsp;然后order&nbsp;by&nbsp;<br>变通2&nbsp;查询时增加字段排序字段&nbsp;例子<br>select&nbsp;aa,bb,cc&nbsp;,ord=&nbsp;case&nbsp;cc&nbsp;when&nbsp;null&nbsp;then&nbsp;1&nbsp;else&nbsp;0&nbsp;end&nbsp;from&nbsp;table&nbsp;order&nbsp;by&nbsp;ord,cc&nbsp;&nbsp;&nbsp;(这边&nbsp;null&nbsp;我没测试不知道是否正确,不过意思我表达出来了哈)<br>接分&nbsp;:)
 
后退
顶部