使用表2中的值以一对多关系更新表1备忘录字段

时间:2017-08-09 22:45:17

标签: sql advantage-database-server ansi-sql-92

我有2个表我想用表2中的值更新表1中的一列,其中id = id。但是,表2有许多行匹配表1,表2的所有行都需要更新为表1中的1行

表-A

id |  all_names      |
---+-----------------+
1  |AB CD FG HI      |
2  |                 |  

** Table_B **

id | name  |
---+-------+
1  |       |
2  | Jon   | 
2  | Mike   |

更新后,表1应如下所示

    id |  all_names      |
    ---+-----------------+
    1  |AB CD FG HI      |
    2  |Jon Mike         |  

我试过

update a 
set a.all_names = TRIM(a.all_names) + b.name + ' '
from table_a a, table_b b
where a.id = b.id

我最终获得的是table_a

中的空all_names

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

除了循环之外,我真的看不到其他任何方式。

DECLARE @id int
DECLARE @name varchar(50)

SELECT * INTO #temp FROM TABLE_B

WHILE EXISTS (SELECT 1 FROM #temp)
BEGIN
SELECT @id = (SELECT TOP 1 id from #temp)
SELECT @name = (SELECT TOP 1 [name] from #temp where id = @id)

UPDATE A
SET all_names = LTRIM(RTRIM(all_names + CHAR(32) + @name))
FROM Table_A A
WHERE A.id = @id

DELETE FROM #temp WHERE id = @id and [name] = @name

END

DROP TABLE #temp

查询将表B的内容放入临时表中,并在使用它后删除该行。所以基本上所有名称都通过循环为自己的ID保持相同的值,除了空格+每次添加下一个名称。我已经为更新添加了一个修剪以防止前导/尾随空格。

答案 1 :(得分:0)

我不知道这是否有帮助,但这是一个严格使用SQL的Oracle版本。您没有在要求中提及它,但第二次合并会阻止行中的重复条目:

创建表格并插入示例行

DROP TABLE table_a;
DROP TABLE table_b;

CREATE TABLE table_a
(
    id INTEGER
  , all_names VARCHAR2 (128)
);

CREATE TABLE table_b
(
    id INTEGER
  , name VARCHAR2 (10)
);

INSERT INTO table_a (id, all_names)
     VALUES (1, 'AB CD FG HI');

INSERT INTO table_a (id, all_names)
     VALUES (2, NULL);

INSERT INTO table_b (id, name)
     VALUES (1, NULL);

INSERT INTO table_b (id, name)
     VALUES (2, 'Jon');

INSERT INTO table_b (id, name)
     VALUES (2, 'Mike');

COMMIT;

合并允许重复

MERGE INTO table_a ta
     USING (SELECT DISTINCT id, LISTAGG (name, ' ') WITHIN GROUP (ORDER BY name) OVER (PARTITION BY id) names
              FROM table_b) tb
        ON (ta.id = tb.id)
WHEN MATCHED
THEN
    UPDATE SET all_names   = all_names || tb.names
WHEN NOT MATCHED
THEN
    INSERT     (
               ta.id, ta.all_names
               )
        VALUES (
                   tb.id, tb.names
               );

SELECT *
  FROM table_a;

ROLLBACK;

合并消除重复

MERGE INTO table_a ta
     USING (SELECT DISTINCT id, LISTAGG (name, ' ') WITHIN GROUP (ORDER BY name) OVER (PARTITION BY id) names
              FROM (WITH
                        aset
                        AS
                            (SELECT id, TRIM (all_names) || ' ' AS all_names
                               FROM table_a),
                        bset (id, name, REMAINDER)
                        AS
                            (SELECT id
                                  , SUBSTR (all_names, 1, INSTR (all_names, ' ') - 1) name
                                  , SUBSTR (all_names, INSTR (all_names, ' ') + 1) REMAINDER
                               FROM aset
                             UNION ALL
                             SELECT id
                                  , SUBSTR (REMAINDER, 1, INSTR (REMAINDER, ' ') - 1) name
                                  , SUBSTR (REMAINDER, INSTR (REMAINDER, ' ') + 1) REMAINDER
                               FROM bset
                              WHERE name IS NOT NULL)
                    SELECT id, name
                      FROM bset
                     WHERE name IS NOT NULL
                    UNION
                    SELECT id, name
                      FROM table_b
                     WHERE name IS NOT NULL)) tb
        ON (ta.id = tb.id)
WHEN MATCHED
THEN
    UPDATE SET all_names   = tb.names 
WHEN NOT MATCHED
THEN
    INSERT (ta.id, ta.all_names)
    VALUES (tb.id, tb.names);

SELECT *
  FROM table_a;

--ROLLBACK;

答案 2 :(得分:0)

我最终做了什么

Declare @Crs cursor as select * from Table_B;   //Temp Table 
open @crs;
while fetch @crs do
update Table_A set all_names=ifnull(Table_B,'')+trim(@crs.name)+' ' where 
id=@Crs.id;
end while;
close @crs;

这使用最少的行并且是优雅的