2012-02-23 77 views
1

我想將數據從一個表複製到另一個表。怎麼樣?在一個查詢中訪問3表

$P_studentnumber=mysql_real_escape_string($_POST['studentnumber']); 
$P_subjectcode=mysql_real_escape_string($_POST['subjectcode']); 
$P_semester=mysql_real_escape_string($_POST['semester']); 

$sql "INSERT units INTO grades (units) FROM subjects WHERE subjectcode='$P_subjectcode'"; //<--THIS IS THE PART THAT I WOULD WANT TO ADD 

$sql="INSERT INTO grades (studentnumber,course, subjectcode, semester, instructorname, units) SELECT '$P_studentnumber', course, '$P_subjectcode', '$P_semester','$instructorname', 'units' FROM students WHERE studentnumber='$P_studentnumber'"; 

回答

1

你可以試試:

INSERT table1(col1,col2) SELECT col1,col2 FROM table2 
1

嘗試這樣

INSERT到site_constants_description 選擇site_constants_description_new * FROM site_constants_description_new,site_language WHERE site_language.site_language_id = site_constants_description_new.site_language_id。

歡呼

0

SELECT...INSERT語句的正確的SQL語法是:

INSERT INTO tableA (colA, colB, colC) 
SELECT colA, colB, colC 
FROM tableB 
WHERE colA = 'valueHere' -- this statement here is the condition 

你必須記住,列名(或必須匹配彼此。如果您想要INSERT的列不匹配,您仍然可以INSERT,但您將爲此提供ALIAS

INSERT INTO tableA (colA, colB, colC) 
SELECT fieldA as colA, fieldB as colB, fieldC as colC 
FROM tableB 
WHERE fieldA = 'valueHere' -- this statement here is the condition 

同爲JOINED表:

INSERT INTO tableA (colA, colB, colC) 
SELECT tableB.fieldA as colA, tableB.fieldB as colB, tableC.fieldC as colC 
FROM tableB INNER JOIN tableC ON tableB.fieldA = tableC.fieldA 
WHERE tableC.fieldA = 'valueHere' -- this statement here is the condition