推荐where子句

时间:2013-01-20 08:37:42

标签: php mysql sql

我有以下代码来返回我想要的字符串

$sql = "
    SELECT `description` 
    FROM `auctions` 
    WHERE `description` REGEXP '[0-9]{10}'
";

但是我需要为这样的事情运行更新查询

UPDATE `url` 
FROM `auctions_media`
WHERE `url` = "$sql" AND `auction_id` = SomeNumber

如何匹配表,以使auction_idauctionsauction_id表中的auctions_media匹配?

我希望代码遍历所有行,从描述中返回字符串,然后将其用于更新查询

2 个答案:

答案 0 :(得分:1)

不完全确定你要追求的是什么,但也许就是这样:

UPDATE auctions_media JOIN auctions USING (auction_id)
SET    auctions_media.url = auctions.description
WHERE  auctions.description REGEXP '[0-9]{10}'

或者这个:

UPDATE auctions
  JOIN auctions_media
    ON auctions_media.auction_id = auctions.auction_id
   AND auctions_media.url        = auctions.description
SET    ...
WHERE  auctions.description REGEXP '[0-9]{10}'

答案 1 :(得分:0)

您可以使用MySQL subqueries。你的UPDATE syntax错了。

如果我理解正确,那么你需要这样的东西(使用子查询的例子):

UPDATE `auctions_media` 
SET url = 'to something you fetched before'
WHERE `auction_id` = (SELECT id
                      FROM `auctions` 
                      WHERE `some condition you need`)