从两个表中获取datetime字段的最新数据

时间:2014-06-09 13:20:46

标签: mysql join

此问题与我之前的问题略有不同:Get the latest data comparing datetime fields from two table

我有两张相同结构的表:

表:用户

+----+--------+--------------+----------------------+
| ID | Name   |    Password  |    LastUpdateTime    |
+----+--------+--------------+----------------------+
|  1 | abu    |    abu123NEW |  2014-06-04 14:55:06 |
|  2 | john   |      john123 |  2014-06-04 14:58:22 |
|  3 | shane  |     shane123 |  2014-06-04 15:02:06 |
|  4 | marie  |     marie123 |  2014-06-04 15:00:06 |
|  5 | mike   |   mike123NEW |  2014-06-04 15:01:32 |
|  6 | kiron  |  kiron123NEW |  2014-06-04 15:05:46 |
|  8 | Bruce  |     bruce123 |  2014-06-04 15:07:11 |
+----+--------+--------------+----------------------+

表: user_k

+----+--------+--------------+----------------------+
| ID | Name   |    Password  |    LastUpdateTime    |
+----+--------+--------------+----------------------+
|  1 | abu    |       abu123 |  2014-06-04 14:53:06 |
|  2 | john   |   john123NEW |  2014-06-04 14:59:48 |
|  3 | shane  |  shane123NEW |  2014-06-04 15:00:06 |
|  4 | marie  |  marie123NEW |  2014-06-04 15:03:17 |
|  5 | mike   |      mike123 |  2014-06-04 15:00:36 |
|  6 | kiron  |     kiron123 |  2014-06-04 15:02:18 |
|  7 | Dan    |       dan123 |  2014-06-04 15:09:46 |
+----+--------+--------------+----------------------+

现在我需要从这两个表中获取最新数据(通过'LastUpdateTime'),如下所示:

+----+--------+--------------+----------------------+
| ID | Name   |    Password  |    LastUpdateTime    |
+----+--------+--------------+----------------------+
|  1 | abu    |    abu123NEW |  2014-06-04 14:55:06 |
|  2 | john   |   john123NEW |  2014-06-04 14:59:48 |
|  3 | shane  |     shane123 |  2014-06-04 15:02:06 |
|  4 | marie  |  marie123NEW |  2014-06-04 15:03:17 |
|  5 | mike   |   mike123NEW |  2014-06-04 15:01:32 |
|  6 | kiron  |  kiron123NEW |  2014-06-04 15:05:46 |
|  7 | Dan    |       dan123 |  2014-06-04 15:09:46 |
|  8 | Bruce  |     bruce123 |  2014-06-04 15:07:11 |
+----+--------+--------------+----------------------+

我现在拥有的:

select
u.id,
u.name,
if(u.LastUpdateTime >= k.LastUpdateTime, u.password, k.password) as password,
greatest(u.LastUpdateTime, k.LastUpdateTime) as LastUpdateTime
from
user u
inner join user_k k on u.id = k.id

但是这只会获取两个表都可用的记录...... 我还需要表中任何一个表中的数据。另外,我担心大量数据的查询性能。

以下是 sqlfiddle

PS 除了姓名&密码两个表中还有十几个字段(列)。我只是为了简化问题而避免使用它们。

2 个答案:

答案 0 :(得分:2)

SELECT x.*
  FROM 
     ( SELECT * FROM user
       UNION ALL
       SELECT * FROM user_k
     ) x
  JOIN 
     ( SELECT id,MAX(lastupdatetime) max_lut
         FROM
            ( SELECT * FROM user
               UNION ALL
              SELECT * FROM user_k
            ) a
        GROUP
           BY id
     ) y
    ON y.id = x.id
   AND y.max_lut = x.lastupdatetime
 ORDER
    BY id;

http://sqlfiddle.com/#!2/05d8be/14

答案 1 :(得分:0)

我有一个解决方案:

SELECT * from 

(SELECT u.*
FROM user u LEFT OUTER JOIN user_k uk ON u.ID = uk.ID WHERE 
(uk.ID IS NULL OR u.LastUpdateTime > uk.LastUpdateTime)

UNION ALL

SELECT uk.*
FROM user_k uk LEFT OUTER JOIN user u ON u.ID = uk.ID WHERE 
(u.ID IS NULL OR uk.LastUpdateTime >= u.LastUpdateTime)) usr

order by usr.ID

SQL FIDDLE