I have three table.2 data comes from 2 table and insert it into another table.
Let: tbl_id hold the field id and tbl_msg hold the field msg.
tbl_id tbl_msg
id msg
------ --------
1234 test_msg
5678
9876
Now my desired output should be in tbl_info
id msg
--- -------
1234 test_msg
5678 test_msg
9876 test_msg
I write a query but its showing error that: Subquery returns more than 1 row.
My query is below:
INSERT INTO tbl_info (id,msg) VALUES((SELECT id FROM tbl_id),(SELECT msg FROM tbl_msg))
You need to write a single SELECT statement which returns teh rows exactly as you want them inserted.
The 2 inner selects you have return all the id’s from tbl_id and all the msg from tbl_msg, which doesn’t make much sense.
First write the select, e.g.:
Then use it for the INSERT:
UPDATE according to OP’s comments:
This will not be very efficient because the inner select will be executed for each record in tbl_id. A more efficient solution may be: