2014-01-10 16 views
0

我正在使用php mysql,並且有12個包含學生信息的表。有3個主表第一個表是註冊表,第二個表示註冊表,第三個表示註冊表。demandraft表包含所有creditcard表字段,但爲空。現在我想從這三個表中獲取全部數據來生成我的xls文件,但是因爲在demand表中存在信用卡表的空字段,所以無法從所有3個表中獲取整個記錄。所有3個表格中都有stuid字段。來自表的所有數據不是通過在mysql中加入查詢來的

這裏是我的連接查詢爲:

$sql = "select * from registration 
     join programme on registration.id=programme.stuid 
     join family on registration.id=family.stuid 
     join address on registration.id=address.stuid 
     join education on registration.id=education.stuid 
     join extradetail on registration.id=extradetail.stuid 
     join workexperience on registration.id=workexperience.stuid 
     join demanddraft on registration.id=demanddraft.stuid 
     join payonline on registration.id=payonline.stuid 
     where (DATE(registration.createddate)>='".$term1."' 
     AND DATE(registration.createddate)<='".$term2."')"; 
+0

相反的加入使用左連接 –

回答

1

使用左連接。

select * 
from a join b on a.id = b.a_id 

不會表的表線沒有出現在B表。

select * 
from a left join b on a.id = b.a_id 

會。

將多個錶鏈接在一起時,左連接可能會有點棘手。您可能必須巧妙地在連接周圍使用括號,以便連接的順序是正確的。

1

此:

$sql = "select * from registration 
     left join programme on registration.id=programme.stuid 
     left join family on registration.id=family.stuid 
     left join address on registration.id=address.stuid 
     left join education on registration.id=education.stuid 
     left join extradetail on registration.id=extradetail.stuid 
     left join workexperience on registration.id=workexperience.stuid 
     left join demanddraft on registration.id=demanddraft.stuid 
     left join payonline on registration.id=payonline.stuid 
     where (DATE(registration.createddate)>='".$term1."' 
     AND DATE(registration.createddate)<='".$term2."')"; 

應該做的伎倆

相關問題