Welcome Guest, Not a member yet? Register   Sign In
How to Insert Data Into Join Tables?
#1

I can select and join, but I can't insert with these below tables in the same time. How to insert data into joined tables?

- tbl_category(cate_id, name, description)

- tbl_brand(brand_id, brand_name)

- tbl_product(product_id, product_name, cate_id, brand_id)
Reply
#2

(This post was last modified: 05-25-2016, 11:22 PM by skunkbad.)

I don't think MySQL allows you to do a joined insert, so you'd just do three inserts inside a transaction.
Reply
#3

(This post was last modified: 05-26-2016, 01:41 AM by Phally Seng. Edit Reason: Add specific question )

Yes, I think so. But I find this idea. How to do it in Codeigniter?

Code:
INSERT INTO table3 ( name, age, sex, city, id, number, nationality)
SELECT name, age, sex, city, p.id, number, n.nationality
FROM table1 p
INNER JOIN table2 c ON c.Id = p.Id
INNER JOIN table3 n ON n.Id = p.Id


And I also find trigger. Which one is better to avoid lost data?
Reply
#4

That is different. If you look carefully, what is being inserted is data that is being selected from three other tables. So it's not that the insert is a join, but the insert is OF a join.
Reply
#5

(05-26-2016, 08:52 AM)skunkbad Wrote: That is different. If you look carefully, what is being inserted is data that is being selected from three other tables. So it's not that the insert is a join, but the insert is OF a join.

Thanks for your explanation. I'd need your idea, I'm sorry for my updated question. How can I insert data to multiple tables in sequence at the same time?
Reply
#6

(05-26-2016, 06:34 PM)Phally Seng Wrote:
(05-26-2016, 08:52 AM)skunkbad Wrote: That is different. If you look carefully, what is being inserted is data that is being selected from three other tables. So it's not that the insert is a join, but the insert is OF a join.

Thanks for your explanation. I'd need your idea, I'm sorry for my updated question. How can I insert data to multiple tables in sequence at the same time?

Read this part of the docs:

https://www.codeigniter.com/user_guide/d...tions.html
Reply
#7

(05-26-2016, 07:22 PM)Read this part of the docs: Wrote: https://www.codeigniter.com/user_guide/d...tions.html

Thank you for your docs.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB