Welcome Guest, Not a member yet? Register   Sign In
MySQL Query help
#1

[eluser]andriu[/eluser]
Hi guys

I'm stuck on a sql query

Perhaps some has an idea.

I want to copy a record i "table1":

Code:
INSERT INTO table1(name, price, etc.)
SELECT name, price, etc. FROM table1
WHERE table1.record_id =13

Then I want to copy the records in "table2" related to table1.id = 13
but obvoiusly change these records record_id to the record_id of the new entry in table1.

Code:
Table 2:
id  table1_id  description etc.
1     13         some text


Is there a way to do this in one query =)?? Or am I trying to overdo it and better of doing it with php.

Thanks for any help
#2

[eluser]@rno[/eluser]
Hi Andriu,

The simple answer is: No.
You cannot insert into two tables at once.
But if you want (as I assume you do) insert all related records in table2 at once than the answer is yes.
You could create a simple sql that says:

Code:
INSERT INTO Table2 (table1_id, description)
SELECT YOUR_NEW_ID_HERE, description FROM Table2 WHERE table1_id = 13

obviously you need to replace the YOUR_NEW_ID_HERE with the newly created ID from table1

Good luck!

Arno
#3

[eluser]andriu[/eluser]
Hi again

Seems like it does the job.


Thanks alot!




Theme © iAndrew 2016 - Forum software by © MyBB