将数据从一个表插入到另一个表中,其中两列不匹配

时间:2022-08-04 00:26:50

I have two tables.

我有两张桌子。

TABLE A
ID | LABEL|PARENT_ID| VISIBLE | HIDEABLE
1  |SPORTS|   1     |    1    |    1
2  | FOOD |   1     |    1    |    0
3  | CARS |   1     |    0    |    1
4  |TRAVEL|   1     |    1    |    1
5  | LOVE |   1     |    1    |    1
6  | OTHER|   1     |    1    |    0
7  | SHOES|   3     |    0    |    1
8  |TRAVEL|   3     |    1    |    1
9  |NATURE|   3     |    1    |    1
10 |FRIEND|   3     |    1    |    0
11 | CARS |   3     |    0    |    1
12 | LOVE |   3     |    1    |    1

TABLE B
ID | LABEL|PARENT_ID| DISPLAY_POST | DISPLAY_COMMENTS
1  |SPORTS|   1     |         1    |    1
2  | FOOD |   1     |         1    |    0
3  | CARS |   1     |         0    |    1
4  |TRAVEL|   3     |         1    |    1

I want to insert data into TABLE B from TABLE A with these checks:

我想通过这些检查将数据从表A插入表B:

A.LABEL <> B.LABEL AND A.VISIBLE=1 AND A.HIDEABLE=1

A.LABEL <> B.LABEL AND A.VISIBLE = 1 AND A.HIDEABLE = 1

How can I do this?

我怎样才能做到这一点?

Everything I try it returns duplicates or missing rows.

我尝试的所有东西都会返回重复或丢失的行。

1 个解决方案

#1


0  

Just use insert . . . select with the right conditions:

只需使用插入。 。 。选择合适的条件:

insert into b( . . .)
     select . . .
     from a
     where a.visible = 1 and a.hideable = 1 and
           not exists (select 1 from b2 where a.label = b2.label);

The . . . are just the list of columns for inserting.

这个。 。 。只是插入列的列表。

#1


0  

Just use insert . . . select with the right conditions:

只需使用插入。 。 。选择合适的条件:

insert into b( . . .)
     select . . .
     from a
     where a.visible = 1 and a.hideable = 1 and
           not exists (select 1 from b2 where a.label = b2.label);

The . . . are just the list of columns for inserting.

这个。 。 。只是插入列的列表。