简体   繁体   English

来自表的整个数据不是通过mysql中的连接查询来的

[英]the whole data from tables is not coming via join query in mysql

i'm working with php mysql and there are 12 tables which contains student informations.There are 3 main table First table is registration, second is demandraft and third is creditcard.The demandraft table contains all the creditcard table fields but as empty. 我正在使用php mysql,有12个表包含学生信息。有3个主表第一个表是注册,第二个是demandraft,第三个是creditcard。demandraft表包含所有信用表表字段但是为空。 now i want to get the whole data from these three tables to generate my xls file but coz there are empty fields of creditcard table in demandraft table so unable to fetch the whole records from all 3 tables. 现在我想从这三个表中获取整个数据来生成我的xls文件,但是因为在demandraft表中有一些空字段的信用表,所以无法从所有3个表中获取整个记录。 there is stuid field common in all 3 tables. 在所有3个表中都有共同的字段。

Here is my join query for that: 这是我的连接查询:

$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."')";

Use a left join. 使用左连接。

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

will not list lines of table a that do not appear in table b. 不会列出表b中未出现的表格a的行。

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

will. 将。

Left join might be a bit tricky when chaining many of them with multiple tables. 当使用多个表链接其中许多时,左连接可能有点棘手。 You may have to cleverly use parentheses around joins such that the order of joins is correct. 您可能必须巧妙地在连接周围使用括号,以使连接顺序正确。

This : 这个 :

$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."')";

Should do the trick 应该做的伎俩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM