doujiao7483
2013-11-17 00:13 阅读 42
已采纳

如何创建虚拟表或如何在同一行中按日期排序

Hi i have this in my database.

id | status1 | status2 | time1 | time2 | time3  | time4  | time5   | userid | itemuid
 1 |    2    |    1    | 000001| 000003| 000008 |   0    | 000099  |   15   |   620
 2 |    2    |    1    | 000002| 000004| 000001 |   0    | 000002  |   620  |   15

Now i need to echo some notifications the problem is time is in the same row.. so its a big huge problem to get this in order for example my conditions would look like

if ($actualuser == $userid && $status1==2)
echo  notification 
echo time4

if ($actualuser == $userid && $status2==1)
echo  notification 
echo time2

This goes insinde a for each loop and the only problem is i need to echo from latest(newest) to oldest, like the example below... In this examples the idea would set or Order all the times as it was just one time columns.. is this posibble?

item id1 time(000099) 
item id1 time(000008)
etc
item id1 time(000001)
item id1 time(0)
item id2 time(0)

how can i do this? Bye the way this 2 conditions are just for example.. i have like 4 or 6 conditions using the same format, but maybe i cant do this this way.. what can i do to set notifications for this...

thanks.. Im lost

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doutuan6158 doutuan6158 2013-11-17 00:30

    So, As I said in comments you need to do as folow:

    select t.*, t2.times from yourtable t, (
        select id, time1 times from yourtable union
        select id, time2 from yourtable union
        select id, time3 from yourtable union
        select id, time4 from yourtable union
        select id, time5 from yourtable ) t2
     where t.id = t2.id
     order by t2.times desc
    
    点赞 评论 复制链接分享

相关推荐