我有一个哺乳动物属性表,包含以下元素;
MammalAttributeID AttributeID MammalID有些哺乳动物的属性记录为“猫”(AttributeID 234),有些哺乳动物的属性记录为“猫”(AttributeID 456),有些哺乳动物有“猫”和“猫”的属性记录。
我们希望将哺乳动物属性表中的"cat“属性替换为"cat”,并删除“cat”属性。因此,如果一只哺乳动物只有“猫”属性记录,它将保持不变。如果它同时具有"cat“和"cat”属性记录,则删除“cat”属性记录。
如果只有"cat“属性记录,则插入"cat”属性记录,并删除“cat”属性记录。
我无法用必要的SQL来完成这一任务。某个SQL专家能为我指明正确的方向吗?
发布于 2014-07-10 17:15:45
我认为Olgas的反应会忽略哺乳动物是猫而不是猫的情况,因为删除是先发生的。
--update cats to felines
Update "mammal attribute table"
set AttributeID = 456
where AttributeID=234
go
--delete cat attribute
delete from attributetable where AttributeID=234
go
--delete duplicate feline attribute
delete
from "mammal attribute table"
where mammalattributeid in(
select max(mammalattributeid)
from "mammal attribute table"
group by mammalid,attributeid
having count(*)>1)发布于 2014-07-10 17:08:54
步骤1.删除带有“cat”属性的行,用于具有“猫”属性的哺乳动物。
DELETE FROM [YOUR_TABLE]
WHERE MammalID in
(SELECT MammalID FROM [YOUR_TABLE] WHERE AttributeID = 234 -- cat
INTERSECT
SELECT MammalID FROM [YOUR_TABLE] WHERE AttributeID = 456) --feline
AND AttributeID = 234第二步:将所有剩余的“猫”替换为“猫”。
UPDATE [YOUR_TABLE]
SET AttributeID = 456
WHERE AttributeID = 234发布于 2014-07-10 16:10:34
假设这些列位于不同的列中,则需要规范第一列,因此它们都是相同的,然后只需删除另一列。
Update [table] set [attribute1] = 'cat' where [attribute1]='feline' or [attribute2]='cat' 然后,删除所有其他:
delete from [table] where [attribute2]='feline' or [attribute2]='cat'这样就行了。
https://stackoverflow.com/questions/24681206
复制相似问题