[英]Combining sql queries in PHP
您好目前使用此代碼
$qry_display = "
SELECT
student_id,
fname,
sex,
lname,
mname,
level,
photo,
birth_date,
birth_place,
address,
father,
father_occupation,
father_phone,
father_company,
father_degree,
mother,
mother_occupation,
mother_phone,
mother_company,
mother_degree,
adviser_id
from
tbl_enroll
where
student_id='$id'
AND level='$lvl'
";
$sql_display = mysql_query($qry_display) or die (mysql_error());
上面的代碼從tbl_enroll獲取大部分數據。 現在我想獲取一些關於tbl_er的數據。 tbl_enroll和tbl_er與student_id的主鍵連接,也連接到tbl_section 。 tbl_er和tbl_section與section_id的外鍵相連。
到目前為止,我一直在考慮做多個SQL查詢並使用一個mysql_query觸發器,但它不會工作,因為觸發器不能使用三個SQL查詢。
只需JOIN
三個表:
SELECT t1.*, t2.*, t3.*
from tbl_enroll t1
JOIN tbl_el t2 ON t1.student_id = t2.student_id
JOIN tbl_section t3 ON t2.section_id = t3.section_id
where student_id='$id' AND a.level='$lvl'
你應該加入這些表格。 這不是關於php而是關於sql。
您可以嘗試按照systax
select tb1.filds,tb2.fields ... from table1 as tb1, table2 as tb2 .. where tb1.id = tb2.id and tb2.id = tb3.id ...
在哪里條件必須正確檢查。
否則你可以使用加入查詢... .Join查詢更好
SELECT te.XXX,tr.YYYY,ts.ZZZ, .....
from tbl_enroll te
inner join tbl_er tr on tr.student_id = te.student_id
inner join tbl_section ts on ts.section_id = te.section_id
where student_id='$id' and level='$lvl';
您可以在查詢開頭選擇te,tr或ts中的任何字段。 不要忘記在te,tr或ts字段前加上前綴,以便在執行查詢時沒有不明確的引用;)
Combining two queries..
SELECT t1.* FROM tbl_er as t1, tbl_enroll as t2
WHERE t1.student_id= t2.student_id ORDER BY id DESC
您必須在兩個表中定義學生ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.