sql—如何检查两个mysql表中两次的差异?

t9eec4r0  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(337)

所以我有两张table:
集成电路:

id | created
---|--------------------
1  | 2016-03-31 16:20:03
2  | 2016-03-31 16:25:18
3  | 2016-03-31 16:28:09

状态:

id | ic_id | timestamp
---|--------------------
1  | 1     | 2016-03-31 16:20:03
2  | 5     | 2016-03-31 16:25:18
3  | 5     | 2016-03-31 16:28:09

我现在想找出 ic.created 以及 status.timestamp 中的第一个相应记录 status table。我从这个开始:

SELECT status.`timestamp` - informed_consent.created as difference
FROM status
WHERE status.`timestamp` > '2017-06-19' 
AND status.ic_id IN (
    SELECT informed_consent.id
    FROM informed_consent
    WHERE informed_consent.id = status.ic_id; 
);

但我马上得到一个 error in my mysql syntax . 我想我也可以用左连接,但我有点迷路了。
有人能帮我找到正确的方向吗?

u0njafvf

u0njafvf1#

我想你可以用 inner join 而不是 sub query 由于 informed_consent.created 在外部sql中不存在,它在子查询中

SELECT status.`timestamp`,status.`timestamp` - informed_consent.created as difference
FROM status
JOIN informed_consent ON informed_consent.id = status.ic_id
WHERE status.`timestamp` > '2017-06-19'

相关问题