如何比较sql中两个表的数据?

时间:2017-05-13 01:48:39

标签: mysql sql

我有这样的表:

  • 表名: registrationdate

enter image description here

我想在last_update的列上获取最大值,所以我使用此查询:

SELECT idtemp, max(last_update) FROM `registrationdate`

然后结果将是这样的:

enter image description here

我有这样的表:

  • 表名: temp

enter image description here

然后我想比较registrationdate's table中的 max(last_update)generatedtime中的temp's table

这是架构:

enter image description here

我可以这样做,以便我可以得到这样的表格?提前谢谢。

idtemp registrationdate's table中的temp's table = id

1 个答案:

答案 0 :(得分:1)

请尝试以下方法......

SELECT ID AS ID,
       generatedTime AS generatedTime,
       tempTable AS tempTable,
       CASE
           WHEN generatedTime < maxLastUpdate THEN
               'not'
           ELSE
               'update'
       END AS UPDATEorNot
FROM temp
JOIN ( SELECT idtemp AS idtemp,
              MAX( last_update ) AS maxLastUpdate
       FROM registrationdate
       GROUP BY idtemp
     ) AS maxLastUpdateFinder ON temp.ID = maxLastUpdateFinder.idtemp;

<强>假设

tempregistrationdate中至少没有匹配记录的记录不需要包含在输出中。

比较和样本数据有效且样本输出的UPDATEorNot字段应为not

<强>解释

此语句以子查询开头,该子查询查找idtempregistrationdate的每个唯一值以及与其关联的last_update的最新值。

然后在INNER JOIN和子查询的结果之间执行temp,以便保留tempregistrationdate中至少有一条相应记录的每条记录,并且其相应的最新值last_update附加到其上。

temp的字段随后被SELECT编辑,CASE语句用于确定要为每个记录的UPDATEorNot字段选择的值。

请注意,当您提供的比较应用于样本数据时,我的语句将返回not。如果您希望选择update,则需要将比较从generatedTime < max( last_update )更改为generatedTime > max( last_update ),或更正您的示例数据。

<强>测试

我的声明是针对使用以下脚本创建的示例数据库进行测试的......

CREATE TABLE registrationdate
(
    last_update   DATETIME,
    idtemp        INT
);
INSERT INTO registrationdate ( last_update,
                               idtemp )
VALUES ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 ),
       ( '2017-05-12 14:20:58', 0 );
CREATE TABLE temp
(
    ID              INT,
    generatedTime   DATETIME,
    tempTable       VARCHAR( 50 )
);
INSERT INTO temp ( ID,
                   generatedTime,
                   tempTable )
VALUES ( 0, '2017-05-01 14:37:00', 'temp_pelamardoktorjk'                ),
       ( 2, '2017-05-01 14:37:00', 'temp_pelamarmagisteripk'             ),
       ( 3, '2017-05-01 14:38:00', 'temp_pelamarmagisterstatusinstitusi' ),
       ( 4, '2017-05-01 14:38:00', 'temp_pelamarmagisterusia'            ),
       ( 5, '2017-05-01 14:38:00', 'temp_pelamarmagisterstatusinstansi'  );

如果您有任何问题或意见,请随时发表评论。

附录1

以下是上述陈述的另一种形式......

SELECT ID AS ID,
       generatedTime AS generatedTime,
       tempTable AS tempTable,
       CASE
           WHEN generatedTime < MAX( last_update ) THEN
               'not'
           ELSE
               'update'
       END AS UPDATEorNot
FROM temp
JOIN registrationdate ON temp.ID = registrationdate.idtemp
GROUP BY ID,
         generatedTime,
         tempTable;

附录2

请尝试以下尝试重写链接代码...

SELECT 
       generatedTime AS generatedTime,
       tempTable AS tempTable,
       CASE
           WHEN ( ( generatedTime < ( SELECT MAX( last_update ) AS maxPelamarmhsdoktor
                                      FROM pelamarmhsdoktor ) ) OR
                  ( generatedTime < ( SELECT MAX( last_update ) AS maxAkdmstmayor
                                      FROM akdmst_mayor ) ) OR
                  ( generatedTime < ( SELECT MAX( last_update ) AS maxIpbmstdepartemen
                                      FROM ipbmst_departemen ) ) OR
                  ( generatedTime < ( SELECT MAX( last_update ) AS maxIpbmstfakultas
                                      FROM ipbmst_fakultas ) ) OR
                  ( generatedTime < ( SELECT MAX( last_update ) AS maxIpbrefjeniskelamin
                                      FROM ipbref_jeniskelamin ) ) OR
                  ( generatedTime < ( SELECT MAX( last_update ) AS maxJenisinstansi
                                      FROM jenisinstansi ) ) OR
                  ( generatedTime < ( SELECT MAX( last_update ) AS maxJenisinstitusi
                                      FROM jenisinstitusi
                                    ) ) ) THEN
               'Yes'
           ELSE
               'No'
       END AS NeedsToUpdateOrNot
FROM generatedTime;