简体   繁体   English

在MySQLi中使用5 INNER JOIN可以吗?

[英]Is it okay to use 5 INNER JOIN in MySQLi?

I have 6 tables: 我有6张桌子:

employee, contact, educ, employment, work, familybg eid is the foreign key taken from employee table. employee, contact, educ, employment, work, familybg eid是从employee表中获取的foreign key Now am having trouble with displaying the results on one of my web pages, file.php. 现在我无法在我的网页之一上显示结果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. 试图做的是显示从6个表中获取的所有值,并将它们与INNER JOIN结合使用,作为与employee_id的关系,并带有雇员的姓名和详细信息。

See the query(though not the exact column names): 查看查询(尽管不是确切的列名):

file.php 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. while($row=mysqli_fetch_assoc($sql))的结果放在while($row=mysqli_fetch_assoc($sql))并将每列分配给我给定的变量名。 IG $name =$row['name'];...etc...etc for me to make it easy to put the results on a table. IG $name =$row['name'];...etc...etc ,以便于将结果轻松放在表中。 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. <table><tr><td><td><?php echo $name; ?></td></td><tr></table>这就是我对其他其余列所做的操作,将它们放置在<table>标记中。

Since I have 6 tables, I created 6 respective <table> which bears all the columns for each of the db tables. 由于我有6个表,因此我分别创建了6个<table> ,其中包含每个数据库表的所有列。 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? 现在想知道,是我使用INNER JOIN使该尝试失败了,还是因为我过度使用<table>标记影响了整个查询? 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? 另外,在我的情况下,我对mysqli_fetch_assoc()表示怀疑,这真的是我需要能够显示行吗? or perhaps there's another query function for mysqli to retrieve rows from the database aside from this? 也许还有另外一个查询函数供mysqli从中检索database行? And oh, btw, I have tried mysqli_fetch_array() but even that seems not working. 哦,顺便说一句,我已经尝试过mysqli_fetch_array(),但即使这样似乎也不起作用。

Any ideas? 有任何想法吗?

If you notice each INNER JOIN is dealt with one at a time and in a consistent "JOIN Link" each time. 如果您发现每个INNER JOIN一次处理一次,并且每次都在一致的“ JOIN Link”中进行处理。 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. 完成第一个INNER JOIN后,即ccon.eid=cemp.eid然后继续进行到下一个ceduc.eid=cemp.eid但要确保所连接的表中的一个与上一个连接相同(有点像跟随另一个的联接。)因此您可以看到,由于表的数量将增加,因此所需表的构建时间也会增加。

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. 注意 :1)在您的sql中,对于内部联接,请不要使用ccon.eid='cemp.eid'使用ccon.eid=cemp.eid是您未获得任何行的原因。对于每个内部联接,请遵循相同的内容。

2) for safer side use left join or right join. 2)为安全起见,请使用左连接或右连接。 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. 如果给定行集,则尝试将所有inner join更改为left join ,然后只有互斥映射可用。

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

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

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