如何在新数据库中包含当前行(即最近的最后一行)?

时间:2016-10-19 07:01:46

标签: mysql

我想在新数据库中插入最近的三行?

  +------------+----------+---------+--------------+--------------+-----------+--------------+--------+------+--------------+
    | time       | userid   | groupid | jobs_running | jobs_pending | job_limit | 
    +------------+----------+---------+--------------+--------------+-----------+--------------+--------+------+--------------+
    | 1476274005 | achandra |         |            4 |            0 |         0 |            
    | 1476274005 | akawle   |         |           52 |           48 |         0 |                    
    | 1476274005 | apatil2  |         |           20 |            6 |         0 |
    | 1476274793 | snagnoor |         |           17 |           67 |          0 |
    | 1476274793 | snatara2 |         |            0 |           54 |         0 |
    | 1476274793 | sthykkoo |         |            9 |          476 |         0 | 

预期产出:

        | 1476274793 | snagnoor |         |           17 |           67 |          0 |
        | 1476274793 | snatara2 |         |            0 |           54 |         0 |
        | 1476274793 | sthykkoo |         |            9 |          476 |         0 | 

1 个答案:

答案 0 :(得分:1)

我认为这个查询会有效。 db2是新数据库,db1是旧的,您必须复制表格

insert into db2.`new_table_name` select * from  db1.old_table order by `time` desc limit 3

PS。此代码未经测试。