2
我想從數據表中刪除多條記錄。問題是,如果我需要刪除3個存款記錄,我不僅需要查找「存款」關鍵字,還需要查找「餘額」關鍵字。MySQL - 嵌套在條件下
TABLE Report:
--------------------------------------------------------------------------------------
| report_id action_id action_name balance received given item_name total|
--------------------------------------------------------------------------------------
| 1 1 Balance 0 10 0 Gold 10 |
| 2 2 Deposit 10 10 0 Gold 20 |
| 3 3 Deposit 20 10 0 Gold 30 |
| 4 4 Balance 0 5 0 Silver 5 |
| 5 5 Deposit 5 5 0 Silver 10 |
| 6 6 Deposit 10 5 0 Silver 15 |
| 7 1 Withdraw 30 0 10 Gold 20 |
.. .....
我有這樣一段代碼:
...
// Empty array for keys (action_id, action_name)
$tempArray = array();
// Generates string like: '(?,"Deposit"),(?,"Deposit")'
$var = implode(',', array_fill(0,count($tempArray), '(?,"Deposit")'));
// Generates query like: DELETE FROM Report WHERE (action_id, action_name) IN ((?,"Deposit"),(?,"Deposit"))
$sql = "DELETE FROM Report WHERE (action_id, action_name) IN (".$var.")";
try{
$db = getConnection();
$stmt = $db->prepare($sql);
$result = $stmt->execute(array_values($tempArray));
...
我所試圖做的事:
...
// Generate string like: '(?,"Deposit" OR "Balance"),(?,"Deposit" OR "Balance")'
$var = implode(',', array_fill(0,count($tempArray), '(?,"Deposit" OR "Balance")'));
// Generate query like: DELETE FROM Report WHERE (action_id, action_name) IN ((?,"Deposit" OR "Balance"),(?,"Deposit" OR "Balance"))
$sql = "DELETE FROM Report WHERE (action_id, action_name) IN (".$var.")";
我想,也許這可能是工作:
...
// Generate string like: '(?,"Deposit","Balance"),(?,"Deposit","Balance")'
$var = implode(',', array_fill(0,count($tempArray), '(?,"Deposit","Balance")'));
// Generate query like: DELETE FROM Report WHERE (action_id, action_name,action_name) IN ((?,"Deposit","Balance"),(?,"Deposit","Balance"))
$sql = "DELETE FROM Report WHERE (action_id, action_name,action_name) IN (".$var.")";
任何歡迎指導。
對不起,默默無聞。我更新了我的問題。 'action_id'是非唯一的標識符。 – mintaras
@ user1288841更新了我的答案 –
它似乎工作正常。謝謝。 – mintaras