Tuesday, February 24, 2015

MySQL INSERT INTO VALUES and SELECT multiples row of data

Found  a way to insert into a table some value i've and one i get from a select from other table


INSERT INTO table1 VALUES ("string", 10, [int]).

I  have the value of ''string" and the 10, but i've to find that [int] from a select like this:

SELECT idTable2
FROM table2
WHERE ...
Solutions:
Use an insert ... select query, and put the known values in the select:

insert into table1
select 'A string', 5, idTable2
from table2
where ...
Issue that may come out:

A problem I've found with this approach is that if the SELECT returns zero records then the statement succeeds but no data is INSERT. –  Neil C. Obremski Feb 13 '14 at 1:52