[英]SQL - Update table changing one column value based on another table
Sorry if the title is not as descriptive as it should be but it is kind of difficult to explain in one sentence what I am trying to do ;). 抱歉,标题的描述不如应有的描述,但是用一句话很难解释我要做什么;)。
I have one table that links parent objects with its respective childs. 我有一个表,将父对象与其各自的子对象链接在一起。 And I have another table with all the objects (parents and childs) with its respectives images.
我还有另一个表,其中包含所有对象(父母和孩子)及其相应的图像。 However, the image is just set for the parents objects.
但是,该图像只是为父对象设置的。 I would like to update this last table and set the childs image the same image that is already set for its parent.
我想更新最后一张表,并将子图像设置为已经为其父图像设置的图像。 Besides, as there is more than one image for each object, I would like to set one in particular, which I can know based on an attribute column.
此外,由于每个对象都有一个以上的图像,因此我想特别设置一个图像,我可以基于一个属性列知道这一点。
My tables look something like: 我的表看起来像:
RelationTable 关系表
child-id 儿童编号
parent-id 父母编号
ImageTable 图像表
object-id 对象编号
attribute-id 属性编号
image-url 图片网址
And here goes an example in order to clarify things: 这里是一个例子,以澄清问题:
RelationsTable 关系表
child-id | 儿童编号| parent-id
父母编号
3 | 3 | 1
1个
4 | 4 | 1
1个
5 | 5 | 2
2
ImageTable 图像表
object-id | 对象ID | attribute-id |
属性ID | image-url
图片网址
1 | 1 | goodimage |
goodimage | image1.jpg
image1.jpg
1 | 1 | badimage |
坏图像| image1b.jpg
image1b.jpg
2 | 2 | goodimage |
goodimage | image2.jpg
image2.jpg
2 | 2 | badimage |
坏图像| image2b.jpg
image2b.jpg
3 | 3 | goodimage |
goodimage | no
没有
3 | 3 | badimage |
坏图像| no
没有
4 | 4 | goodimage |
goodimage | no
没有
4 | 4 | badimage |
坏图像| no
没有
5 | 5 | goodimage |
goodimage | no
没有
5 | 5 | badimage |
坏图像| no
没有
So, I would like to set the images of objects 3, 4 and 5 (child ones) to its respective parent images, but to the 'correct' ones, that is the images with 'goodimage' as attribute-id. 因此,我想将对象3、4和5(子对象)的图像设置为其各自的父图像,但将其设置为“正确”的图像,即以“ goodimage”作为属性ID的图像。
At the end it should look like: 最后应该看起来像:
1 | 1 | goodimage |
goodimage | image1.jpg
image1.jpg
1 | 1 | badimage |
坏图像| image1b.jpg
image1b.jpg
2 | 2 | goodimage |
goodimage | image2.jpg
image2.jpg
2 | 2 | badimage |
坏图像| image2b.jpg
image2b.jpg
3 | 3 | goodimage |
goodimage | image1.jpg
image1.jpg
3 | 3 | badimage |
坏图像| no
没有
4 | 4 | goodimage |
goodimage | image1.jpg
image1.jpg
4 | 4 | badimage |
坏图像| no
没有
5 | 5 | goodimage |
goodimage | image2.jpg
image2.jpg
5 | 5 | badimage |
坏图像| no
没有
Actually, I don't care if 'badimage' is set as well, but the important one is 'goodimage'. 实际上,我也不在乎是否也设置了“ badimage”,但重要的是“ goodimage”。
I've been trying something like: 我一直在尝试类似的东西:
UPDATE ImageTable
SET image = (SELECT image-url FROM ImageTable WHERE ImageTable.object-id = RelationTable.parent-id AND ImageTable.attribute-id = 'goodimage')
WHERE ImageTable.object-id = RelationTable.child-id AND ImageTable.attribute-id = 'goodimage'
but it's not working since it is not correct SQL syntax. 但它不正确,因为它的SQL语法不正确。 I don't know if I should use a variable (never used one) or if this can be done with just one SQL sentence.
我不知道我是否应该使用一个变量(从未使用过),或者是否可以仅使用一个SQL语句来完成。
Any help would be much appreciated. 任何帮助将非常感激。
First of all, if the relation between parent and child is 1:n, why don't you just add that information to the same table? 首先,如果父子关系为1:n,为什么不将这些信息添加到同一表中呢? eg fieldname "parentid" (if it's empty/null it's only a parent).
例如,字段名“ parentid”(如果为空/ null,则仅为父级)。 Benefit: you don't need an additional table and can easily create a hierarchy if needed.
好处:您不需要其他表,并且可以根据需要轻松创建层次结构。
And for the image, I guess you want to display this somewhere in your code, but it should be easier to just modify the SELECT to get the image-url of the parent is no image is given for the child. 对于图像,我猜您想在代码中显示此图像,但是修改SELECT以获取父图像的图像URL应该更容易,因为没有为孩子提供图像。 Benefit: no need to update the table when you get new entries and it would also be possible to for some child entries to have their own image (if needed).
好处:获得新条目时无需更新表,并且某些子条目也可以具有自己的图像(如果需要)。
edit: just noticed your new comment about the open-source project part, of course this makes it hard to change the database design. 编辑:刚注意到您对开源项目部分的新评论,当然这使更改数据库设计变得困难。 But maybe it's still easier to move some of the problems to the programming side (unless this is handled by the open-source software as well).
但是也许将一些问题转移到编程方面还是比较容易的(除非同样由开源软件处理)。
something like this? 这样的东西?
NOTES: 笔记:
This could be merged into one non-subquery statement but I was lazy. 可以将其合并为一个非子查询语句,但是我很懒。
I did not test, expect typos 我没有测试,期待错别字
;WITH goodlist AS
(
SELECT child-id, image-url
FROM relationstable
left join imagetable on relationstable.child-id = relationstable.child-id and attribute-id = "goodimage"
)
UPATE imagetable
set imagetable.image-url =
(SELECT top 1 image-url from goodlist where child-id = imagetable.object-id)
WHERE imagetable.image-url = "no"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.