Here’s a sample of a table I have:
mysql> select * from table_a where description='Auto';
+----+----------+-------------+-----------------+
| id | idparent | description | description_url |
+----+----------+-------------+-----------------+
| 11 | NULL | Auto | NULL |
| 74 | 73 | Auto | NULL |
+----+----------+-------------+-----------------+
2 rows in set (0.00 sec)
mysql>
Now I insert into another table another record and remember its last inserted id:
INSERT INTO table_b (description) values ('Myvalue');
set @lt=LAST_INSERT_ID();
Now I have into @lt the value.
I could do this (and it works):
INSERT INTO table_a_link_b (id_a, id_b) VALUES (11, @lt);
INSERT INTO table_a_link_b (id_a, id_b) VALUES (74, @lt);
Now what follows doesn’t work:
INSERT INTO table_a_link_b (id_a, id_b) VALUES
( (SELECT id FROM table_a where description='Auto') , @lt);
So I tried to put it into a variable:
mysql> set @t=(select id from table_a where description='Auto');
ERROR 1242 (21000): Subquery returns more than 1 row
It doesn’t work. Is there a way I could do it without making a Php batch file and stay pure SQL?
1 Answer