0

我有两张桌子——今晚我真的相信我有一个金发碧眼的时刻。

标题

HeadingID, HeadingName, TopicID

小标题

SubHeadingID, SubHeadingName, HeadingID

我想做的是复制标题中的所有条目,SubHeadings其中TopicID= 1 到 = 2。

我写过:

INSERT INTO Headings (HeadingName, TopicID)
SELECT HeadingName, 2 FROM Headings WHERE TopicID = 1  //(Duplicating Headings and assigning TopicID 2).

我被困在SubHeadings; 我想放入HeadingID刚刚创建的新的,我不知道。我可以使用相同的插入/选择语句来完成其余的字段,但是我很难过。

有人可以帮我举个例子或一些文档吗?

简化后的数据如下所示:

HeadingID = 1
HeadingName = 'News'
TopicID = 1
HeadingID = 2
HeadingName = 'Sport'
TopicID = 1

SubHeadingID = 1
SubHeadingName = 'Local'
HeadingID = 1
SubHeadingID = 2
SubHeadingName = 'National'
HeadingID = 1
SubHeadingID = 3
SubHeadingName = 'International'
HeadingID = 1
SubHeadingID = 4
SubHeadingName = 'Baseball'
HeadingID = 2
SubHeadingID = 5
SubHeadingName = 'Cricket'
HeadingID = 2
4

1 回答 1

0

以下查询将为您提供帮助:)

INSERT INTO SubHeadings (SubHeadingName, HeadingID) 
SELECT subhdng.SubHeadingName, hdng.HeadingID 
FROM Headings hdng
INNER JOIN SubHeadings subhdng ON hdng.HeadingID = subhdng.HeadingID 
WHERE hdng.TopicID = 2
于 2013-01-13T13:09:49.610 回答