我正在尝试更新数据库的多个pages
行以更新菜单的顺序。
pages
有2个需要更新的数据库字段:parent_page_id
和menu_ordering
。
数据库表的id为page_id
。所有字段都是整数。
我创建的SQL如下:
UPDATE `pages` SET parent_page_id = CASE page_id WHEN 256 THEN 0 WHEN 259 THEN 256 WHEN 239 THEN 256 WHEN 242 THEN 0 WHEN 246 THEN 0 WHEN 254 THEN 0 WHEN 255 THEN 0 END, SET menu_ordering = CASE page_id WHEN 256 THEN 0 WHEN 259 THEN 0 WHEN 239 THEN 1 WHEN 242 THEN 0 WHEN 246 THEN 0 WHEN 254 THEN 0 WHEN 255 THEN 0 END WHERE page_id IN (256, 259, 239, 242, 246, 254, 255);
人类可读:
UPDATE `pages`
SET parent_page_id = CASE page_id
WHEN 256 THEN 0
WHEN 259 THEN 256
WHEN 239 THEN 256
WHEN 242 THEN 0
WHEN 246 THEN 0
WHEN 254 THEN 0
WHEN 255 THEN 0
END,
SET menu_ordering = CASE page_id
WHEN 256 THEN 0
WHEN 259 THEN 0
WHEN 239 THEN 1
WHEN 242 THEN 0
WHEN 246 THEN 0
WHEN 254 THEN 0
WHEN 255 THEN 0
END
WHERE page_id IN (256, 259, 239, 242, 246, 254, 255);
可悲的是,这个SQL包含错误。我无法弄清楚为什么? 我已经使用了这个'指南'获取此SQL http://www.karlrixon.co.uk/writing/update-multiple-rows-with-different-values-and-a-single-sql-query/ 我的语法似乎完全相同?
你能帮我找到错误吗?
答案 0 :(得分:1)
您需要删除第二个SET
UPDATE `pages`
SET parent_page_id = CASE page_id
WHEN 256 THEN 0
WHEN 259 THEN 256
WHEN 239 THEN 256
WHEN 242 THEN 0
WHEN 246 THEN 0
WHEN 254 THEN 0
WHEN 255 THEN 0
END,
menu_ordering = CASE page_id
WHEN 256 THEN 0
WHEN 259 THEN 0
WHEN 239 THEN 1
WHEN 242 THEN 0
WHEN 246 THEN 0
WHEN 254 THEN 0
WHEN 255 THEN 0
END
WHERE page_id IN (256, 259, 239, 242, 246, 254, 255);
答案 1 :(得分:1)
只有一套
UPDATE `pages`
SET parent_page_id = CASE page_id
WHEN 256 THEN 0
WHEN 259 THEN 256
WHEN 239 THEN 256
WHEN 242 THEN 0
WHEN 246 THEN 0
WHEN 254 THEN 0
WHEN 255 THEN 0
END,
menu_ordering = CASE page_id
WHEN 256 THEN 0
WHEN 259 THEN 0
WHEN 239 THEN 1
WHEN 242 THEN 0
WHEN 246 THEN 0
WHEN 254 THEN 0
WHEN 255 THEN 0
END
WHERE page_id IN (256, 259, 239, 242, 246, 254, 255);