我有兩個表名爲users
和contacts
。我想一次刪除兩張表中的記錄。爲此,我現在使用下面的代碼。如何將兩個PDO查詢合併爲一個?
public function delete($userId, $contactId) {
/* Since Values cannot be mixed (the Numbers) with control flow logic
(the commas) with prepared statements needs one placeholder per Value. */
//As many question marks as array entries; the last one needs no comma
$questionMarks = str_repeat("?,", count($userId)-1)."?";
$sth = $this->dbh->prepare("DELETE FROM users WHERE id IN($questionMarks)");
$sth->execute($userId);
$questionMarks = str_repeat("?,", count($contactId)-1) . "?";
$sth = $this->dbh->prepare("DELETE FROM contacts WHERE id IN($questionMarks)");
$sth->execute($contactId);
}
請注意$userId
和$contactId
將是一個陣列和計數始終是相等的。
如何將這兩個查詢合併爲一個?
答:
下面的查詢爲我工作。
DELETE users,contacts FROM users INNER JOIN contacts WHERE users.id IN (2) AND contacts.id IN (2);
AHHA查詢的方式,能解決我的問題,是如何愚蠢的我不給MySQL文檔中查找相關要求前,還有謝謝爲了您的幫助,我使用了下面的代碼,它可以工作。 DELETE用戶,聯繫人用戶INNER JOIN聯繫人WHERE users.id IN(2)AND contacts.id IN(2); – 2011-04-28 09:13:05
您可能需要添加ON子句或Join。 – Pentium10 2011-04-28 09:17:49
這將會更好,我猜。再一次感謝你 :) – 2011-04-28 09:21:56