How to insert a value based on a search from another table?

I need to find a way to make INSERT INTO table A , but one of the values ​​is what comes from the search in table B, let me illustrate.

I have the following two tables:

Table a:

 A1: String A2: Integer value coming from table B A3: More Data 

Table B:

 B1: String B2: Integer Value 

Example line A: {"Value", 101, MoreData} Example line B: {"English", 101}

Now I know that I need to INSERT the following in {"Value2", "English", MoreData}, but obviously this will not work, because the integer in the second column is not expected to mean the word "English", so I you must first search in table B.

Something like that:

 INSERT INTO tableA (A1, A2, A3) VALUES ("Value2", SELECT B2 FROM tableB where B1="English", MoreData); 

Obviously, this does not work as it is ...

Any suggestions?

+7
sql insert-into
source share
2 answers

What about:

 Insert into tableA ( a1,a2,a3) (select "value2", b2, moreData from TableB where B1 = "English") 
+6
source share

There seems to be more cross-DBMS without parentheses, as @Andriy M commented:

 insert into tableA (a1, a2, a3) select 'value2', b2, moreData from tableB where B1='English' 
+1
source share

All Articles