sophie
sophie

Reputation: 1521

Get the value from other table to insert into table that related?

I have 3 table with related each other

table1 (for insert)
*******************
id1 |   lable    |     line1

table2
********************
id2 |   id1  |  id3

table3
*******************
id3 | line1

How can write in the SQL(MySQL) script instead do php script ?

INSERT INTO table1 (id1,lable,line1) VALUES ($from_GET, $from_GET,How can I got from table3?);

Thanks

Upvotes: 1

Views: 84

Answers (2)

Aleks G
Aleks G

Reputation: 57326

If you're doing inserts, then you can do:

INSERT INTO table1 (id, label, line1)
SELECT ... FROM ... WHERE ...

You can also do an update referencing related tables:

UPDATE table1
JOIN table2 ON ...
SET table1.field = table2.another_field

Note that if you're using $_GET variables, it's EXTREMELY important that you clean that input to prevent sql injection attacks.

Upvotes: 1

vyegorov
vyegorov

Reputation: 22895

INSERT INTO table1 (id1,lable,line1)
SELECT $from_GET, $from_GET, line1 FROM table3 WHERE id3 = $from_GET;

Check the syntax in the docs.

Upvotes: 2

Related Questions