doujiushi9007 2014-08-07 06:35
浏览 137
已采纳

MySQL排序多个时间戳

I have 2 table in my database. They are admin action table and user action. And have table structure as follows ::

Admin Action table

Action_id |  order    |   Action_type    |           timestamp          |   
   1          12            pending           2014-07-24 15:30:45
   2          12            packing           2014-08-24 14:12:11 
   3          12            shipping          2013-05-01 15:55:12
   4          12            packing           2011-11-25 21:10:22

User Action table

Action_id |  order    |   Action_type    |           timestamp          |   
   1          12            transfer           2014-07-22 15:30:45
   2          12            transfer           2014-09-23 14:12:11 
   3          12            transfer           2013-05-07 15:55:12
   4          12            transfer           2011-11-28 21:10:22

I want to sort timestamp both table. What is come before or after? And Finally. There will be result as follows:

Activity table

Action_id     |   Action_type      |                 timestamp           |
    4               packing                    2011-11-25  21:10:22      //admin action 
    4               transfer                   2011-11-28  21:10:22      //user aciton
    3               shipping                   2013-05-01  15:55:12      //admin action
    3               transfer                   2013-05-07  15:55:12      //user aciton 
    1               transfer                   2014-07-22  15:30:45      //user aciton
    1               pending                    2014-07-24  15:30:40      //admin action
                       .....

What is MySQL command to slove this?

I know I can coding an algorithm in PHP for sort timestamp. But I think if good query from database. It helps to make it easy to handle in the next step.

  • 写回答

1条回答 默认 最新

  • dongwen1935 2014-08-07 06:38
    关注

    This untested query should give you the expected result:

    select * from (
    Select * from `Admin Action`
    union all
    Select * from `User Action`) as a
    order by timestamp          
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?