I have s MySQL database and I need to insert some specific data in a table. The data should be as follows:
SELECT id FROM a_table WHERE … returns me a list of ids.
I need to insert n rows in second_table where n is the count of the returned rows from the first query. The second table requires 2 fields – The first one will be a record from the first query and the second one will be an integer, that I will pass from my script.
For example: If the first query returns (12,14,17,18) and the integer from my script is 5 I need to create a query, that will insert (12,5),(14,5),(17,5),(18,5) and I need this done in the database layer – I don’t want to create a select statement, then create a query and then run it.
I need something like this (this is not a real query – It just shows what I need):
INSERT INTO second_table (user_id,group_id) VALUES ((12,14,17,18),5)
or to be more precise like this:
INSERT INTO second_table (user_id,group_id) VALUES ((SELECT id FROM a_table WHERE …),5)
Is there a way to do this in SQL only (no tsql – sql only)
You can include a literal value in a SELECT: