I’m trying to perform an INSERT INTO statement like the following:
insert into table1 (id, data1, data2) select c.id, c.data1, (select a.id
from table3 a, table2 b where b.id=c.id and a.data3=b.data3) from table2 c;
Basically what’s happening here is I’m migrating some data from one table into another. However, in the new table I want the id of a row from a third table rather than the String that was stored in the original table.
I feel like my query is close, however when I run it mysql is giving the error:
ERROR 1242 (21000): Subquery returns more than 1 row
I think the problem is b.id=c.id but I don’t understand why c.id would not be a specific value. If I replace c.id with a known id, the query functions as expected.
More generally, I need to be able to access the ‘current row’ that I’m on from the results of my select statement and use it’s id in that inner select statement.
How can I make my inner select statement only reference one row?
This is MySql 5.0.77 running on CentOS.
We can’t tell you why your subquery returns more than one row without seeing more of your tables / data. Do some investigating and find which record(s) are duplicated and you should be able to resolve your problem.
If you don’t care about duplicates and you just want the query to work, use
LIMIT 0,1on your subquery