简体   繁体   中英

Is it okay to use 5 INNER JOIN in MySQLi?

I have 6 tables:

employee, contact, educ, employment, work, familybg eid is the foreign key taken from employee table. Now am having trouble with displaying the results on one of my web pages, file.php. What am trying to do is to show all the values taken from each of the 6 tables and combine them together with the use of INNER JOIN as a relation to the employee_id which bears the name and minimal details of the employees.

See the query(though not the exact column names):

file.php

 $sql=mysqli_query($db,"
SELECT cemp.eid
    , cemp.fname
    , cemp.mname
    , cemp.lname
    , cemp.age
    , cemp.gender
    , cemp.birthday
    , cemp.birthplace
    , cemp.citizenship
    , cemp.status
    , cemp.sss
    , cemp.philhealth
    , cemp.tin
    , cemp.height
    , cemp.weight
    , con.address
    , con.province
    , con.postcode
    , con.telno
    , con.mobile
    , ccon.email
    , ccon.alternate
    , ceduc.elem
    , ceduc.egrad
    , ceduc.high
    , ceduc.hgrad
    , ceduc.college
    , ceduc.cgrad
    , cems.position
    , cems.hireDate
    , cems.job_desc
    , cems.basic
    , cems.salary
    , cw.company_name
    , cw.position
    , cw.desc
    , cw.startDate
    , cw.endDate
    , cfam.fatherName
    , cfam.motherName
    , cfam.sibling
    , cfam.spouse
    , cfam.children 
 FROM ch_employee cemp 
INNER 
 JOIN contact ccon 
   ON ccon.eid = 'cemp.eid'
INNER 
 JOIN educ ceduc 
   ON ceduc.eid = 'cemp.eid' 
INNER 
 JOIN employment cems  
   ON cems.eid = 'cemp.eid'
INNER 
 JOIN work cw 
   ON cw.eid = 'cemp.eid'
INNER 
 JOIN family_bg cfam 
   ON cfam.eid = 'cemp.eid' 
WHERE cemp.eid = '$id';
");

The result ofcourse was placed under while($row=mysqli_fetch_assoc($sql)) and assigned each column to my given variable names. IG $name =$row['name'];...etc...etc for me to make it easy to put the results on a table. Like this:

<table><tr><td><td><?php echo $name; ?></td></td><tr></table> <table><tr><td><td><?php echo $name; ?></td></td><tr></table> That's what I did to other remaining columns, placing them towards the <table> tag.

Since I have 6 tables, I created 6 respective <table> which bears all the columns for each of the db tables. I'll show you an example:

            <table>
            <thead>
            <tr>
            <th><strong>Name:</strong></th>
            <th><strong>Address:</strong></th>
            <th><strong>Contact#:</strong></th>
            <th><strong>Email:</strong></th>
            </tr>
            </thead>
            <tbody>
            <tr>
            <td><?php echo $name; ?></td>
            <td><?php echo $address; ?></td>
            <td><?php echo $contact; ?></td>
            <td><?php echo $email; ?></td>
            </tr>
            </tbody>

But everytime I run this code, no results can be displayed. As if I only made a with empty rows. Now am wondering, is it the way I used the INNER JOIN made this attempt unsuccessful or could it be because of my excessive use of <table> tag that affects the whole query? Also, I'm being suspicious with mysqli_fetch_assoc() in my situation is it really what I need to be able to display the rows? or perhaps there's another query function for mysqli to retrieve rows from the database aside from this? And oh, btw, I have tried mysqli_fetch_array() but even that seems not working.

Any ideas?

If you notice each INNER JOIN is dealt with one at a time and in a consistent "JOIN Link" each time. When the first INNER JOIN is done ie ccon.eid=cemp.eid - it then proceeds to move to the next one ceduc.eid=cemp.eid but making sure that one of the tables being joined was the same as the last join (kinda like following the joins one from another).so you can see as the no of tables will increase the time of construction for the required table will also increase.

NOTE :1)in your sql for inner joins do not use ccon.eid='cemp.eid' .use ccon.eid=cemp.eid .this is the reason you are not getting any row .follow the same for each inner join.

2) for safer side use left join or right join. because in inner join if any record won't match you will get no output.

While use inner join all the maping should have values oter wise it wont give output.

Try to change all inner join to left join if it give row set then there are mutually exclusive mapping only available.

So your query will be like follows.

$sql=mysqli_query($db,"SELECT cemp.eid,cemp.fname,cemp.mname,cemp.lname,cemp.age,cemp.gender,cemp.birthday,cemp.birthplace,
cemp.citizenship,cemp.status,cemp.sss,cemp.philhealth,cemp.tin,cemp.height,cemp.weight,con.address,con.province,
con.postcode,con.telno,con.mobile,ccon.email,ccon.alternate,ceduc.elem,ceduc.egrad,ceduc.high,ceduc.hgrad,ceduc.college,ceduc.cgrad,
cems.position,cems.hireDate,cems.job_desc,cems.basic,cems.salary,cw.company_name,cw.position,cw.desc,cw.startDate,cw.endDate,
cfam.fatherName,cfam.motherName,cfam.sibling,cfam.spouse,cfam.children FROM ch_employee AS cemp 

LEFT JOIN contact AS ccon ON ccon.eid='cemp.eid'
LEFT JOIN educ AS ceduc ON ceduc.eid='cemp.eid' 
LEFT JOIN employment AS cems ON cems.eid='cemp.eid'
LEFT JOIN work AS cw ON cw.eid='cemp.eid'
LEFT JOIN family_bg AS cfam ON cfam.eid='cemp.eid' WHERE cemp.eid='$id'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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