在SQL Server中我会使用一个公用表表达式。但是,因为它看起来像你使用MySQL,我会做这样的事情:
select t2.name, t2.room, t2.eventtime, t2.time_from_test
from the_table t1
inner join
(
select name, room,
min(eventtime) as eventtime,
(eventtime - currenttime) > 0 as time_from_test
from the_table
where the_table.room in ('ZACH102', 'ZACH103')
group by name, room
order by time_from_test
) t2 on t2.room = t1.room and t2.name = t1.name
最终你会需要动态设置此,你不知道你的IN (...)
条款的内容提前。到了这个时候,最好的办法是提前计划,以便数组已经存储在某个表中。这会给你两个选择。首先是窝在你的IN (...)
条件的子查询:
select t2.name, t2.room, t2.eventtime, t2.time_from_test
from the_table t1
inner join
(
select name, room,
min(eventtime) as eventtime,
(eventtime - currenttime) > 0 as time_from_test
from the_table
where the_table.room in (select room from shopping_cart where session_key='MySessionKey')
group by name, room
order by time_from_test
) t2 on t2.room = t1.room and t2.name = t1.name
另一种是使用连接:
select t2.name, t2.room, t2.eventtime, t2.time_from_test
from the_table t1
inner join
(
select name, room,
min(eventtime) as eventtime,
(eventtime - currenttime) > 0 as time_from_test
from the_table
inner join shopping_cart on shopping_cart.room = the_table.room
where shopping_cart.session_key = 'MySessionKey'
group by name, room
order by time_from_test
) t2 on t2.room = t1.room and t2.name = t1.name
你是如何传递数组?它是一个SQL数组还是使用不同的语言,然后调用该函数? – Gage 2012-03-16 18:21:32
我有一个来自数据库查询的结果,我在这里解析php中的信息,并将房间的名称放入数组中。然后,我想要使用该数组进行上面的查询 – maknelly 2012-03-16 18:24:10
另外它time_from_test是必要的,还是您只需要从当前系统时间查找下一个事件? – Gage 2012-03-16 19:54:44