我正在尝试为计划任务创建一些php和sql,该任务将在一个值上连接2个表,并根据table2中的unix时间戳值从table1中删除所有记录。到目前为止,我已经提出了以下代码,但是不想运行它,因为它可能会造成更大的损害。有人可以帮忙吗?预先感谢
$sql = "DELETE FROM tbl_code
LEFT JOIN ( SELECT * FROM `tbl_ctoon` WHERE tbl_code.ctoon_id=tbl_ctoon.ctoon_id AND tbl_code.code_for='C')
WHERE DATEDIFF( date(NOW( )) , FROM_UNIXTIME(expire_date)) > 3 ";
$del = new database();
$del->myquery($sql, 1);
因此最终它将合并两个表,并从{{1}中的tbl_code
超过3天的expire_date
中删除所有记录。
答案 0 :(得分:0)
您应该在yle子查询中加入表以获得有效结果
DELETE tbl_code
FROM tbl_code
INNER JOIN (
SELECT tbl_ctoon.expire_date , tbl_ctoon.ctoon_id
FROM `tbl_ctoon`
LEFT JOIN tbl_code ON tbl_code.ctoon_id=tbl_ctoon.ctoon_id
AND tbl_code.code_for='C'
) t2 ON t2.ctoon_id =tbl_code.ctoon_id
AND DATEDIFF( date(NOW( )) , FROM_UNIXTIME(t2. expire_date)) > 3
WHERE tbl_code.code_for='C'
要测试涉及的行,您可以
SELECT *
FROM tbl_code
INNER JOIN (
SELECT tbl_ctoon.expire_date , tbl_ctoon.ctoon_id
FROM `tbl_ctoon`
LEFT JOIN tbl_code ON tbl_code.ctoon_id=tbl_ctoon.ctoon_id
AND tbl_code.code_for='C'
) t2 ON t2.ctoon_id =tbl_code.ctoon_id
AND DATEDIFF( date(NOW( )) , FROM_UNIXTIME(t2. expire_date)) > 3
WHERE tbl_code.code_for='C'
答案 1 :(得分:0)
在删除之前,我会选择一个查询。 您的查询看起来更简单:
$sql = "SELECT * FROM tbl_code
LEFT JOIN tbl_ctoon
ON tbl_code.ctoon_id = tbl_ctoon.ctoon_id AND tbl_code.code_for = 'C'
WHERE DATEDIFF(DATE(NOW()), FROM_UNIXTIME(expire_date)) > 3 AND tbl_ctoon.ctoon_id IS NOT NULL";
然后,如果您的查询返回了您希望删除的条目,请执行删除操作:
$sql = "DELETE FROM tbl_code
LEFT JOIN tbl_ctoon
ON tbl_code.ctoon_id = tbl_ctoon.ctoon_id AND tbl_code.code_for = 'C'
WHERE DATEDIFF(DATE(NOW()), FROM_UNIXTIME(expire_date)) > 3 AND tbl_ctoon.ctoon_id IS NOT NULL";
答案 2 :(得分:0)
由于JOIN无效,所以我走了一条不同的路线。我运行了它,并在phpmyadmin中为我的数据库很好地执行了操作。
rdfFolder = rdsSession.GetDefaultFolder(rdoDefaultFolders.olFolderInbox)