我在底部的第二个查询中收到错误。知道为什么吗?
由于
错误: 错误号码:1093
You can't specify target table 'menus' for update in FROM clause
WORKS:
INSERT INTO menus
(name, controller, parent)
VALUES
('A', 'B', (SELECT id FROM menus menus_alias WHERE SHA1(menus_alias.id) = '1'))
两个都不工作:
UPDATE menus SET
parent = (SELECT id FROM menus menus_alias WHERE SHA1(menus_alias.id) = '1')
WHERE SHA1(id) = '5'
UPDATE menus menus_alias SET
menus_alias.parent = (SELECT id FROM menus WHERE SHA1(id) = '1')
WHERE SHA1(menus_alias.id) = '5'
查看了这些:
和其他一些人
答案 0 :(得分:3)
使用JOIN
UPDATE menus m1 JOIN menus m2
ON SHA1(m1.id) = '5' AND SHA1(m2.id) = '1'
SET m1.parent = m2.id
答案 1 :(得分:1)
你可以试试这个
UPDATE menus SET
parent =(
SELECT id FROM (
SELECT id FROM menus
) AS x
WHERE SHA1(id) = '1'
)
WHERE SHA1(menus_alias.id) = '5'
答案 2 :(得分:0)
尝试此解决方法(两个查询必须在同一个mysql会话中执行):
SELECT
id INTO @za_id
FROM
menus
WHERE
SHA1(id) = '1';
UPDATE menus
SET parent = @za_id
WHERE
SHA1(id) = '5';
此外,您正在尝试使用同一个表中的值更新表,并且由于mysql不知道所选数据来自不同的记录,因此应更新的记录会引发错误。