使用INNER JOIN命令使用OFFSET命令更新多个变量

时间:2012-08-02 22:02:03

标签: mysql

我需要编辑此代码:

UPDATE favorites SET category='$newcat',subcategory='$newsubcat',
sitename='$newwebsitename',
url='$newwebsiteurl' WHERE email='$email' ORDER BY favoritesid ASC LIMIT 1

添加:'OFFSET 1'到最后,但因为UPDATE不允许OFFSET命令, 我需要使用INNER JOIN来添加该部分。但是我只知道怎么做 删除单个变量时:

DELETE a FROM favorites a
INNER JOIN
(
  SELECT favoritesid
  FROM favorites
  WHERE email = '$email'
  ORDER BY favoritesid
  LIMIT 1 OFFSET x

 ) b ON a.favoritesid = b.favoritesid

但是当更新多个变量时,我甚至知道从哪里开始。谁能帮我吗?

1 个答案:

答案 0 :(得分:0)

也许是这样的:

UPDATE favorites A INNER JOIN
(
    SELECT favoritesid FROM favorites WHERE email='$email'
    ORDER BY favoritesid LIMIT x
) B USING (favoritesid)
SET
    A.category='$newcat',
    A.subcategory='$newsubcat',
    A.sitename='$newwebsitename',
    A.url='$newwebsiteurl'
;

这是另一种选择:

UPDATE favorites A
SET
    A.category='$newcat',
    A.subcategory='$newsubcat',
    A.sitename='$newwebsitename',
    A.url='$newwebsiteurl'
WHERE A.favoritesid = 
(
    SELECT MAX(favoritesid) FROM favorites WHERE email='$email'
    ORDER BY favoritesid LIMIT x
);

这些示例假设OFFSET 0.如果您有OFFSET,请尝试以下方法:

UPDATE favorites A INNER JOIN
(
    SELECT favoritesid FROM favorites WHERE email='$email'
    ORDER BY favoritesid LIMIT x OFFSET $myoffset
) B USING (favoritesid)
SET
    A.category='$newcat',
    A.subcategory='$newsubcat',
    A.sitename='$newwebsitename',
    A.url='$newwebsiteurl'
;