我有两张桌子,transforms
和design_fields
。transforms
有一个名为 的字段transforms
,并且design_fields
有一个名为 的字段transform
。design_fields
包含所有transform
行,其中一些是重复的。transforms
(table) transform
(column) 是我试图用transform
来自design_fields
.
我一直在使用以下查询手动完成其中的一些。
获取design_fields
transform
不在transforms
(表)transform
(列)中的列表:
select transform, COUNT(*) as trans_count
from design_fields
where transform not in (
select transform
from transforms
where transform is not null
)
group by transform
order by trans_count desc
插入(表)(列)+其他东西design_field
transform
。transforms
transform
insert into transforms (field_id, transform_name, transform)
select field_id, logical_name, 'TRANSFORM NAME GOES HERE' as transform
from fields
where field_id in (
select field_id
from design_fields
where transform = 'TRANSFORM NAME GOES HERE'
)
尽管此方法在大多数情况下都有效,但我需要在 600 多行中执行此操作。这就是为什么我想使用动态查询将该列表中的所有行插入到transforms
表中。
这是我到目前为止所拥有的(免责声明:我是动态查询的新手):
select 'insert into transforms (field_id, transform_name, transform)
select field_id, logical_name, ' +
(select distinct transform
from design_fields
where transform not in (
select transform
from transforms
where transform is not null ))
+ ' as transform
from fields
where field_id in (
select field_id
from design_fields
where transform = ' +
(select distinct transform
from design_fields
where transform not in (
select transform
from transforms
where transform is not null )) +
')'
此语句返回一条错误消息,指出它返回了超过 1 个值。我明白为什么会这样,我只是不知道有其他方法可以做到这一点。
任何帮助,包括将来使用动态查询的指针,将不胜感激!