日期:2014-05-17  浏览次数:20874 次

一组数据如何按照加入购物车时间排序?
网站
将商品加入购物车
商品ID 肯定是 乱序的了, 比如 
ID号分别是【23,42,41,76,15,90,124,980】
用户将(ID号分别为23,42,41……的)商品添加到购物车,

我希望购物车里的商品列表是按照 添加到购物车里的时间先后顺序来排列,

而不是按照商品的ID来排列。

请问有什么方法?

sql="select * from [product] where ID in(23,42,41,76,15,90,124,980) order by what?"

------解决方案--------------------
如果没有记录添加时间的话,就做n次查询
sql = "select * from [product] where ID = 23" &_
" UNION ALL select * from [product] where ID = 42" &_
" UNION ALL select * from [product] where ID = 41" &_
" UNION ALL select * from [product] where ID = 76" &_
" UNION ALL select * from [product] where ID = 15" &_
" UNION ALL select * from [product] where ID = 124" &_
" UNION ALL select * from [product] where ID = 980"

------解决方案--------------------
回复于:2012-04-16 04:13:52

楼上真勤勉。。。

cookie内只是按照添加顺序拼接的id,在拼接sql时,按这个顺序拼接个临时顺序

SQL code

select a.* from [product] a left join (select 23 as id,1 as orderNumber union select 42 as id,2 as orderNumber union ....) b on a.id=b.id where ID in(23,42,41,76,15,90,124,980) order by b.orderNumber

------解决方案--------------------
HTML code

<%
' idStr 为从cookie取出的id列表
idStr = "23,42,41,76,15,90,124,980"
ids = split(isStr,",")
' idOrder 为临时拼接的排序表
idOrder = ""
for i = 0 to ubound(ids)
   idOrder = idOrder & " union select " & ids(i) & " as id," & i & " as orderNumber"
next
idOrder = right(idOrder,len(idOrder) - 7)
Sql = "select a.* from [product] a left join (" & idOrder & ") b on a.id=b.id where ID in(" & idStr & ") order by b.orderNumber"
%>