Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql更新数据检查不存在的多表_Php_Mysql - Fatal编程技术网

Php mysql更新数据检查不存在的多表

Php mysql更新数据检查不存在的多表,php,mysql,Php,Mysql,我有五张桌子。如何更新一个数据,以检查此数据是否已存在于所有5个表中 我知道一些类似于插入的东西。。。关于重复键更新,但未找到多表的示例 现在我使用一些糟糕的查询,如: mysql_query("UPDATE table1 SET image = '' WHERE image = '".$image."' "); mysql_query("UPDATE table2 SET image = '' WHERE image = '".$image."' "); mysql_query("UPDATE

我有五张桌子。如何更新一个数据,以检查此数据是否已存在于所有5个表中

我知道一些类似于插入的东西。。。关于重复键更新,但未找到多表的示例

现在我使用一些糟糕的查询,如:

mysql_query("UPDATE table1 SET image = '' WHERE image = '".$image."' ");
mysql_query("UPDATE table2 SET image = '' WHERE image = '".$image."' ");
mysql_query("UPDATE table3 SET image = '' WHERE image = '".$image."' ");
mysql_query("UPDATE table4 SET image = '' WHERE image = '".$image."' ");
mysql_query("UPDATE table5 SET image = '' WHERE image = '".$image."' ");
mysql_query("UPDATE table1 SET image = '".$image."' WHERE id = '".$id."'");
首先为数据重复的每个表更新空值,然后插入值。我想这会花更多的钱。。。那么,如何使用较少的查询来进行此更新呢?谢谢

EDIT1:尝试了以下操作,它将更新值ignore以检查该值是否已在五个表中的一个表中退出

mysql_query("
UPDATE table1,table2,table3,table4,table5 
SET table1.image='".$image."' 
WHERE table1.id='".$id."' 
AND table1.image!='".$image."' 
AND table2.image!='".$image."' 
AND table3.image!='".$image."' 
AND table4.image!='".$image."' 
AND table5.image!='".$image."'
");

好吧,我想这就是你想要的

update table1 AS t1
         LEFT JOIN table1 AS t1copy
           ON t1copy.image = '$image'       
   set t1.image = '$image'
 where t1.id = $id
   and t1copy.image IS NULL
   and not exists(select 1 from table2 where table2.image = '$image' )
   and not exists(select 1 from table3 where table3.image = '$image' )

为什么有5个完全相同的表?如果图像不在5个表中的任何一个表中,那么应该将其插入到哪个表中?@ypercube,我正在为图像拇指创建作业工作。5个表的结构不同,但我想避免重复的拇指图像url插入到不同的表中。谢谢。你可以使用类似于
UPDATE。。。在不存在(…)和不存在(…)和…
的情况下,将
imageURL
放在一个表中(上面有唯一的索引)并在所有5个表中仅引用ID会更有意义。感谢您的回答,但我只想将
$image
插入
表1.image
并避免从(
table1.image
table2.image
table3.image
table4.image
table5.image
)。不要将值插入2个表中,谢谢。它不会插入任何表中。它执行与前5个查询相同的操作。我选择这样做是因为您对所需内容的解释不清楚,但您的代码说明得很准确。很抱歉,我无法很好地解释我的问题,因为我的
EDIT1
代码需要更新一个值进入
table1
但我想先检查:如果这个值已经在一个五个表中,不要更新。如果所有五个表都没有这个值,请更新。希望您能理解并帮助我,尊敬的。这导致了错误
您不能在FROM子句中为更新指定目标表“table1”
啊,好的。在这种情况下,我没有答案。