我真的很爲這個任務苦苦掙扎,所以任何幫助或指導都將非常感激。MySQL INNER JOIN 3個表格,也使用INNER JOIN DELETE使用
的表格:
module_uploads
+--------------------------+
| upload_id | upload_name |
+--------------------------+
| 1006 | 12.png |
| 1007 | 18.png |
+--------------------------+
module_timelimit
+------------------------------------------------+
| upload_id | email | expires |
+------------------------------------------------+
| 1006 | [email protected] | 2014-05-22 16:34:54 |
| 1007 | [email protected] | 2015-05-20 20:34:54 |
+------------------------------------------------+
module_fieldvals
-----------------------------------------+
| upload_id | fld_id | value |
-----------------------------------------+
| 1006 | 2 | [email protected] |
| 1006 | 0 | mcdda8fbr |
| 1007 | 0 | mcdda8fbr |
| 1007 | 2 | [email protected] |
-----------------------------------------+
行,所以我想加入基於在到期日已經通過upload_id的3個表,刪除文件,發送電子郵件,然後刪除所有數據。我有2個問題:
- 我只能似乎加入使用fld_id = 2,他們按到期日的表。能夠刪除fld_id = 2和fld_id = 0的數據會很好,但我好像得到了重複的電子郵件,因爲while循環迭代了4次而不是2次。
- 我無法確定如何刪除數據
- 有沒有辦法只用一個SQL查詢來完成它?
這裏是我到目前爲止的代碼:
$database = cmsms()->getDb();
$now = $database->DbTimeStamp(time());
$query = "
SELECT
module_uploads.upload_id,
module_uploads.upload_name,
module_fieldvals.upload_id,
module_fieldvals.fld_id,
module_fieldvals.value,
module_timelimit.upload_id,
module_timelimit.email,
module_timelimit.expires
FROM
module_timelimit
INNER JOIN module_uploads
ON module_timelimit.upload_id = module_uploads.upload_id
INNER JOIN module_fieldvals
ON module_timelimit.upload_id = module_fieldvals.upload_id
WHERE module_timelimit.expires < $now AND module_fieldvals.fld_id = 2
GROUP BY module_timelimit.expires";
$result = mysql_query($query);
// Delete the rows
$query_deletion = "
DELETE cms_module_uploads_timelimit, cms_module_uploads, cms_module_uploads_fieldvals
FROM
cms_module_uploads_timelimit
INNER JOIN cms_module_uploads
ON cms_module_uploads_timelimit.upload_id = cms_module_uploads.upload_id
INNER JOIN cms_module_uploads_fieldvals
ON cms_module_uploads_timelimit.upload_id = cms_module_uploads_fieldvals.upload_id
WHERE cms_module_uploads_timelimit.expires < $now AND cms_module_uploads_fieldvals.fld_id = 2
GROUP BY cms_module_uploads_timelimit.expires";
$delete_result = mysql_query($query_deletion);
// Check if any queries failed
if(!$result || !$delete_result) {
if (!$result) exit("Error - The selection query did not succeed");
if (!$delete_result) exit("Error - The deletion query did not succeed");
} else {
// set up the settings for FTP to delete file
$ftp_server = 'xxxxxxx';
$ftpuser = 'xxxxxx';
$ftppass = 'xxxxxx';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);
if ((!$conn_id) || (!$login_result)) {
echo "Error could not connect to FTP";
} else {
while ($row = mysql_fetch_array($result)) {
$upload_id = $row['upload_id'];
$file = $row['upload_name'];
$email = $row['value'];
// Check if file and email exists
if(isset($file) && isset($email)) {
$file = '/uploads/adverts/'.$file;
// Delete file
if (ftp_delete($conn_id, $file)) {
echo "$file deleted successfully\n";
} else {
echo "could not delete $file\n";
}
// Send email using CMSMS GCB
$cmsmailer->Send();
}
}
}
// close the connection
ftp_close($conn_id);
}
你能不能幫我寫呢?我不知道我在做什麼是正確的! – Jonathan
如果'GROUP BY'以某種方式無法被新用戶使用,那將會很不錯。 – Strawberry
??不知道你的意思 – Jonathan