开发者

Can you evaluate information based on a sub select in SQL?

I want to insert information into a table only if a particular column entry value is less than or equal to another column value in a parent record. Is this possible with SQL,开发者_运维百科 and if so an example would be very much appreciated.


Yes. With the scant information you've provided, this is the best I can do:

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'

The select from parent_table will return one row or no rows - resulting in one row or no rows being inserted.


Look at these 2 examples, I hope they make sense to you and you can understand them.

insert into table_name (cola, colb, colc)
values 
select (colx,coly,colz) from another_table
where another_table.colw<=some_value

or, if the information you are trying to insert does not come from the child table ...

insert into table_name (cola, colb, colc)
values 
select value1,value2,value3
where exists (select 1 from another_table where.colw<=some_value)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜