[英]mysql IF column EXISTS THEN delete record from table
如果 columnID=X,我需要從表中刪除一條記錄,但只能從具有 columnID 的表中刪除。
$query = "show tables" ;
list($selCount,$tables,$selError) = dbSelect($query,array()) ;
foreach ($tables as $table) {
$tableName = $table['Tables_in_dbName'] ;
$query = "IF EXISTS (
SELECT * FROM information_schema.columns
WHERE table_name = '$tableName'
AND column_name='cID'
AND table_schema= DATABASE()
) THEN DELETE FROM $tableName WHERE cID=?;
END IF;" ;
list($delCount,$delError) = dbDelete($query,array($corpID)) ;
}
我的查詢的 IF EXISTS...THEN 部分出了點問題,但我不明白是什么。 我已經閱讀了許多其他 IF EXISTS 示例,但看不出我做錯了什么。
具體來說,我收到以下錯誤:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'clubTabl' at line 1
我已經測試了查詢的內部部分,即SELECT
部分,它工作正常,所以與IF EXISTS
相關的一些問題
我想出了另一種看起來更簡單的方法:
$query = " select table_name from information_schema.columns where column_name='cID'" ;
list($selCount,$tables,$selError) = dbSelect($query,array()) ;
foreach ($tables as $table) {
$tableName = $table['table_name'] ;
$query = "DELETE FROM $tableName WHERE cID=?" ;
list($delCount,$delError) = dbDelete($query,array($corpID)) ;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.