我有一个名为Series
的表格 ID NAME
1 generic
2 irregular
3 regular
另一张表"章节"其中包含列" series_id"及其当前为NULL(适用于所有行)。
id title description series_id 1 Types produced Types of data produced NULL 2 Data standard Data and metadata standards NULL 3 Policies for access Policies for access and sharing NULL 4 Products of Research Products of Research NULL 5 Expected info Expected information NULL 6 Period of retention Period of data retention NULL
我需要按顺序更新具有Series表的id的Sections表,并重复循环直到Section行的结尾。例如,
id title description series_id 1 Types produced Types of data produced 1 2 Data standard Data and metadata standards 2 3 Policies for access Policies for access and sharing 3 4 Products of Research Products of Research 1 5 Expected info Expected information 2 6 Period of retention Period of data retention 3
我想出了一个初始查询,但它将所有行的series_id设置为1。另外,我不确定如何为剩余的行重复series_id
UPDATE sections t1
JOIN series t2
ON t1.series_id = t2.id
SET t1.series_id = t2.id
WHERE t1.series_id IS NULL;
感谢任何指导。提前谢谢。
答案 0 :(得分:0)
这可能会有所帮助:
SET @s = MAX(id) From Series;
UPDATE sections t1
JOIN series t2
ON MOD(t1.id, @s) = MOD(t2.id, @s)
SET t1.series_id = t2.id
WHERE t1.series_id IS NULL;
答案 1 :(得分:0)
您不能像这样加入两个表,因为没有共享列。 您可以使用具有额外列的唯一键,该列对于1,2,3的所有组(例如A,B,C,D,E ....)具有不同的值,这将自动将series_id增加到1,2,3。这可能是最快的解决方案。在更新series_id之后,可以删除额外列。 另一种方法是编写一个过程并使用一个循环。