2016-06-19 79 views
0

我必須表tbl1和tbl2,他們之間沒有關係,我想從兩個表中刪除不同的數據使用一個查詢是可能或應該我做它在兩個單獨的查詢php mysql從兩個不相關的表中刪除數據在一個查詢

我嘗試下面的查詢

$del =" 
DELETE 
    FROM tbl1 
    , tbl2 
WHERE tbl1.delete_time IS NOT NULL 
    AND tbl1.delete_time != '0000-00-00 00:00:00' 
    AND tbl2.delete_time IS NOT NULL 
    AND tbl2.delete_time != '0000-00-00 00:00:00' 
";     
$sql =mysql_query($del) or die(mysql_error()); 
$res = mysql_query($sql); 

,但我得到了以下錯誤: 您在您的SQL語法錯誤;檢查對應於您MariaDB的服務器版本使用附近的正確語法手冊「WHERE tbl1.delete_time IS NOT NULL和tbl1.delete_time!=‘0000-0’在行1

在此先感謝

回答

-1

考慮通過與;像下面

DELETE FROM tbl1 WHERE delete_time IS NOT null AND delete_time != '0000-00-00 00:00:00'; DELETE FROM tbl2 WHERE delete_time IS NOT NULL AND delete_time != '0000-00-00 00:00:00'; 

(OR)推薦的方式分離改變你的查詢;在存儲過程中總結兩個DELETE聲明像

create procedure usp_deleteData 
as 
begin 
    DELETE FROM tbl1 
    WHERE delete_time IS NOT null 
    AND delete_time != '0000-00-00 00:00:00'; 

    DELETE FROM tbl2 
    WHERE delete_time IS NOT NULL 
    AND delete_time != '0000-00-00 00:00:00'; 
end 

而且,儘管你有表之間沒有關係,但你有共同的列名爲delete_time,所以你可以執行Multi Table DELETE-JOIN

DELETE tbl1, tbl2 FROM tbl1 
INNER JOIN tbl2 
WHERE tbl2.delete_time = tbl1.delete_time 
AND tbl2.delete_time IS NOT NULL 
AND tbl1.delete_time IS NOT NULL 
AND '0000-00-00 00:00:00' NOT IN (tbl1.delete_time, tbl2.delete_time); 
+0

我可以使用它php, – george

+0

@george,是的,你可以像調用查詢一樣調用過程。查看PHP文檔相同。 – Rahul

相關問題