2012-06-25 65 views
1

我有3個表彼此相關從其他表中獲取值插入相關的表中?

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

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

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

在SQL(MySQL的)如何編寫腳本,而不是做PHP腳本?

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

感謝

+0

不知道你是什麼意思。 你想從選擇中插入一個[插入](http://dev.mysql.com/doc/refman/5.0/en/insert-select.html)嗎? –

回答

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

檢查syntax in the docs

1

如果你正在做的插入,那麼你可以做:

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

你也可以做一個更新引用相關表:

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

請注意,如果你使用$ _GET變量,清理該輸入以防止SQL注入攻擊非常重要。

相關問題