作者ylk (YLK)
看板Database
标题Re: [SQL ] 请教这个问题如何下SQL呢?
时间Sun Jun 25 15:43:13 2006
※ 引述《weisor (无所事事)》之铭言:
: ※ 引述《ylk (YLK)》之铭言:
: : id used time
: : --------- ------- --------
: : A 5 13:30
: : B 5 13:00
: : C 4 14:00
: : D 1 12:00
: : E 0 11:00
: : 各位前辈好,想请教一下一个SQL问题。
: : 需求:找到 time 时间最早的资料,不过,如果 used 有 > 0 的,要优先选择。
: : 以上列资料表来看,我要选择的是 资料D。
: : 想了很久,还是想不出来这个 SQL 要怎麽下,请问有人能帮忙提醒一下吗?
: : 谢谢!
: 只取一笔
: 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 这个栏位代表的意思是?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.224.29.198