-1

我尝试使用一个mysql查询通过特定参数更新表中的所有值我有这个结构:

seller_id | product | value 
1 | apple | 100
1 | onion | 50
1 | cherry | 20
1 | lemon | 200
2 | apple | 10
2 | onion | 40
2 | lemon | 100
3 | apple | 10
3 | lemon | 50
3 | orange | 140

通常我使用多个查询进行更新:一个选择和多个更新,但我只想进行一个查询以在大数据库中获得更好的性能

SELECT `product` FROM table WHERE seller_id = '1' AND `value` > '0'

while($r = mysql_fetch_row($query))
{
   $list[] = $r['0'];
}

foreach($list as $k => $i)
{
   UPDATE `table` SET `value`='100' WHERE product = '$i'
    // updated: apple:100; onion:100; cherry:100; lemon:100; for all seller which have it
}

我尝试从中进行一个查询,我需要将所有产品存储在卖家:1 中并更新所有拥有该产品的卖家的一些值

我尝试了类似的方法,但它不起作用(#1093 - You can't specify target table 'table' for update in FROM 子句)

UPDATE table SET value='100' WHERE product = 
  (SELECT `product` FROM table WHERE seller_id = '1')

我需要更高的性能,我不想使用 PHP 来做到这一点

4

3 回答 3

2

SQLFiddle

UPDATE tab_name AS t1
  INNER JOIN tab_name AS t2 ON t1.product = t2.product
    SET t1.value = 100
    WHERE t2.seller_id = 1;
于 2013-10-30T19:38:16.690 回答
0

您必须为您的子选择提供新别名或其他方式直接放置 where 条件

UPDATE table SET value='100' WHERE product IN(
 SELECT t.`product` FROM  (SELECT `product` FROM table WHERE seller_id = '1') t )
于 2013-10-30T19:39:33.390 回答
0

关于什么:

UPDATE table1 AS a
INNER JOIN table1 AS b ON b.product = a.product
SET a.value = 100
WHERE  b.seller_id = 1;

它将按产品加入并在其中更新 Seller_id = 1。

sqlfiddle demo

于 2013-10-30T19:47:46.343 回答