我必须创建迁移以更新问题图像属性,但我需要检查其他表中的一些属性,我的架构如下所示:
question {
id,
name,
groupId,
imageUrl -> i need to update this
}
group {
id,
name,
quizId
}
quiz {
id,
name,
type -> where type is 'History'
}
我需要从表格问题更新属性imageUrl WHERE测验类型是例如'历史记录',我不确定在使用UPDATE时如何加入此表格。我试过这样的事情,但它没有像我想的那样工作。
UPDATE question SET image_url = 'pathToImage' FROM quiz q WHERE q.type = 'History'
答案 0 :(得分:1)
所以这是我提出的最佳解决方案,它对我的迁移非常有效。
UPDATE question SET image_url = 'https://path_to_image.com' WHERE id IN (SELECT q.id FROM question q
JOIN group AS g ON q.groupi_id = g.id
JOIN quiz AS qu ON qu.id = g.quiz_id
WHERE qu.type = 'Lifestyle')
所以基本上我将image_url更新为子查询中包含ID的所有问题,这是最简单的方法。
答案 1 :(得分:0)
以这种方式加入三个表:
UPDATE question q
SET image_url = 'pathToImage'
FROM (select g.Id
from group g
JOIN quiz z
ON g.quizID = z.Id
WHERE z.type = 'History') p
ON q.groupId = p.Id;
答案 2 :(得分:0)
尝试:
with cte as (
select q.id qid
from question q
join "group" g on g.id = groupId
join quiz z on z.id = quizId
where type = 'History'
)
UPDATE question q
SET image_url = 'pathToImage'
FROM cte.qid = q.id