Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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,看看我的sqlfiddle: (编辑) 我试图执行的代码: UPDATE inventory SET product_name = CONCAT((SELECT name FROM products_and_packaging WHERE product_id = (SELECT product_id FROM inventory WHERE inventory_id = 196)), ' 100g Bunch') WHERE inventory_id = 196

看看我的sqlfiddle:

(编辑)

我试图执行的代码:

UPDATE inventory SET 
    product_name = CONCAT((SELECT name FROM products_and_packaging 
    WHERE product_id = (SELECT product_id FROM inventory 
    WHERE inventory_id = 196)), ' 100g Bunch')
WHERE inventory_id = 196
我要查找的结果是库存表中的
产品名称
从NULL重命名为“Flowers 100g Bunch”,其中产品id=196

我得到了#1093错误(不能在FROM子句中为更新指定目标表“inventory”)

注意:这里有类似问题的答案:


然而,我尝试应用这些解决方案,由于建议的内部联接,我发现了完全相同的错误。任何指向正确方向的指针都很好。

您不需要嵌套子查询。只需连接两个表,如链接问题所示

UPDATE inventory AS i
JOIN products_and_packaging AS pp ON pp.product_id = i.product_id
SET i.product_name = CONCAT(pp.name, ' 100g Bunch')
WHERE i.inventory_id = 196

啊,这是一个多么完美的解决方案啊!。。。这很好用。我还在WHERE子句中使用了这个解决方案,而不是在一个全新的子查询中!