2017-04-08 119 views
0

我有employeeinfo表和employeeloan表.. 和我使用此查询:PHP的MySQL:连接两个表

$result=mysqli_query($con,"select tblemployeeinfo.employeeid, tblemployeeloan.amount from tblemployeeinfo left join tblemployeeloan on tblemployeeinfo.employeeid = tblemployeeloan.employeeid where tblemployeeinfo.employeeid = <employee id for example employee id 1>"); 

while($row = mysqli_fetch_array($result)){ 
echo "<br>".$row['employeeid']; 
echo "<br>".$row['amount']; 
} 

结果是

employee id: 1 
amount: 1000 
employee id: 1 
amount: 500 
employee id: 1 
amount: 100 

,但我想要的是像显示这个:

employee id: 1 
amount: 1000 
amount: 500 
amount: 100 

抱歉我的英文不好。 TNX提前

+1

你确定你的结果有员工ID?我没有看到你在查询中选择了 – hassan

+0

对不起,先生...我的坏我会编辑我的帖子..据说这是tblemployeeid.employeeid –

+0

[左连接没有重复行左表]的可能重复( http://stackoverflow.com/questions/22769641/left-join-without-duplicate-rows-from-left-table) –

回答

0

普通黑客可以通过下面的代码来完成

$i = 0 ; 
while($row = mysqli_fetch_array($result)){ 
    if($i == 0) 
     echo "<br>".$row['employeeid']; 
    echo "<br>".$row['amount']; 
    $i++; 
} 
+0

谢谢先生..实际上它的工作..我现在将在我的代码中使用它..但如果有其他方式,我很高兴知道它..反正tnx再次先生.. –

+0

也可以检查如果mysql可以做工作和解决这个问题 - > SELECT whatever_is_needed FROM table1 INNER JOIN table2 ON table1.otherid2 = table2.otherid2 GROUP BY whatever_id – OldPadawan