我想仅在特定列条目值小于或等于父记录中的另一列值时才将信息插入表中。这是否可以通过SQL实现,如果是这样的话,我将非常感激。
答案 0 :(得分:1)
是。由于您提供的信息很少,这是我能做的最好的事情:
insert into child_table (col1, col2, col3, etc)
select 'value1', 'value2', 'value3', etc
from parent_table
where parent_id = 'parent_id_value'
and parent_col1 <= 'somevalue'
从parent_table中选择将返回一行或没有行 - 导致一行或没有插入行。
答案 1 :(得分:0)
看看这两个例子,我希望它们对你有意义,你可以理解它们。
insert into table_name (cola, colb, colc)
values
select (colx,coly,colz) from another_table
where another_table.colw<=some_value
或者,如果您尝试插入的信息不是来自子表......
insert into table_name (cola, colb, colc)
values
select value1,value2,value3
where exists (select 1 from another_table where.colw<=some_value)