我有2个表,import_links和import_links_history
我有一个php文件从import_links中获取行,运行一个cron作业,当它完成后,它会在import_links_history中使用import_links.id和时间戳插入一行,所以我现在就完成了。 cron工作每天都在运行。
如何选择今天没有条目的行?
结构摘要
import_links
- id
- other meaningless details
import_links_history
- id
- import_links_id
- created_at
DDL
CREATE TABLE import_links
(
`id` int,
`productName` varchar(7)
)
;
CREATE TABLE import_links_history
(
`id` int,
`import_links_id` int,
`created_at` timestamp
)
;
当前查询
SELECT import_links.id,import_links_history.created_at
FROM import_links
JOIN import_links_history
ON import_links.id=import_links_history.id
WHERE DATE(import_links_history.created_at) <> DATE(NOW())
ORDER BY import_links_history.created_at DESC
上面的查询是我的尝试,但它返回现在日期的所有行。我需要从今天所有的行中返回那些不存在的行。
有人可以帮忙吗?
答案 0 :(得分:0)
<强>计划强>
- 获取与当前日期匹配的链接历史记录中的ID
- 从ID不在上面的链接中选择记录
示例输入
+----+-----------------+--------------------------+
| id | import_links_id | created_at |
+----+-----------------+--------------------------+
| 1 | 1 | August, 03 2015 14:49:10 |
| 1 | 1 | August, 02 2015 14:49:10 |
| 2 | 3 | August, 03 2015 14:49:10 |
| 3 | 2 | July, 02 2015 14:49:10 |
+----+-----------------+--------------------------+
<强>查询强>
SELECT *
FROM import_links
where id not in
(
select distinct id
from import_links_history
where DATE(import_links_history.created_at) = current_date
and id is not null
)
;
<强>输出强>
+----+-------------+
| id | productName |
+----+-------------+
| 3 | OpenDJ |
+----+-------------+
与current_date August, 03 2015 00:00:00