作者weisor (无所事事)
看板Database
标题Re: [SQL ] 请教这个问题如何下SQL呢?
时间Sun Jun 25 16:23:08 2006
※ 引述《ylk (YLK)》之铭言:
: ※ 引述《weisor (无所事事)》之铭言:
: : 只取一笔
: : select * from T
: : order by case when (used > 0) then 1 else 0 end desc, time asc
: : limit 1
: : 恶心的写法
: : select * from T
: : join (
: : select case when used > 0 then 1 else 0 end as u, min(time)as t from T
: : group by case when used > 0 then 1 else 0 end) x
: : on x.u = case when used > 0 then 1 else 0 end and x.t = T.time
: : where (exists(select * from T where used > 0) and used > 0)
: : or (not exists(select * from T where used > 0) and used = 0)
: 谢谢您的指导。
: 想请问您,您的环境是? case 这个栏位代表的意思是?
case statement
mysql, postgres, db2, mssql 都有支援
请参考
http://msdn2.microsoft.com/zh-tw/library/ms181765.aspx
http://www.postgresql.org/docs/8.1/interactive/functions-conditional.html
http://publib.boulder.ibm.com/infocenter/db2luw/v8/
topic/com.ibm.db2.udb.doc/admin/r0005646.htm
http://dev.mysql.com/doc/refman/5.1/en/case-statement.html
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.104.29.35