简体   繁体   English

左联接工作不正常

[英]LEFT JOIN Not working properly

I need some help with LEFT Join 我需要一些有关LEFT Join帮助

first db is "fpo" 第一个数据库是“ fpo”

id----empnum-----amount---date----ponum ID ---- empnum -----量---日期---- ponum

second db is "users" 第二个数据库是“用户”

id----amount-----empid----username----password ID ---- -----量---- EMPID名----密码

I need to total the "amount" in fpo for each user and compare the total to the amount from users. 我需要总计每个用户在fpo中的“金额”,并将总计与用户的金额进行比较。

This is the code i have now that is NOT working. 这是我现在无法使用的代码。

 $result = mysqli_query($con,
 "select fpo.amount , fpo.empnum , user.amount , user.fpo SUM(amount) 
  FROM LEFT JOIN fpo ON fpo.empnum = users.empnum GROUP BY empnum");

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

For some reason this is not working.... What am i doing wrong i have never worked with JOIn command. 由于某种原因,这不起作用。...我在做什么错呢?我从未使用过JOIn命令。

Here is what the data looks like in mysql 这是mysql中数据的样子

**first db is "fpo"**
id----empnum-----amount-----date-------ponum
1-----854245-----5.00------9/7/14------12345
2-----123987-----8.00------9/7/14------12345
3-----123987-----5.00------9/7/14------12345
4-----854245-----15.00-----9/7/14------12345
5-----548798-----10.00-----9/7/14------12345
6-----854245-----30.00-----9/7/14------12345

**second db is "users"**
id----amount-----empid----username----password
1-----700.00-----854245---admin-------abc123
2-----500.00-----123987---admin-------abc123
3-----200.00-----548798---admin-------abc123

What i am trying to do is sum(amount) From fpo where empnum (note:from fpo) = empnum (note:from users) then echo the summed amount with the empnumber and the amount from the users table. 我想做的是从fpo的sum(amount) ,其中empnum(note:from fpo)= empnum(note:from users)然后用empnumber和用户表中的数量回显求和的数量。

I need to do this for everyone in the users db. 我需要为用户数据库中的每个人执行此操作。 I have about 150 people in there right now..... 我现在大约有150个人。

Thank you for your time and help!!! 谢谢您的时间和帮助!!! Probably a simple problem :( 可能是一个简单的问题:(

You're not echoing the summed amount, $row['amount'] is the amount of a single row, you want $row['SUM(amount)'] . 您没有回显总金额, $row['amount']是单行的金额,您需要$row['SUM(amount)'] I suggest you give it an alias to make it easier to access: 我建议您给它起一个别名,以使其更易于访问:

$result = mysqli_query($con,"select f.amount , u.empid , u.amount , 
                                u.fpo, SUM(u.amount) AS total
                             FROM users AS u
                             LEFT JOIN fpo AS f ON f.empnum = u.empid
                             GROUP BY u.empid") or die(mysqli_error($con));

while($row = mysqli_fetch_array($result)) {
      echo $row['empnum'];
      echo "<br>";
      echo $row['total'];
}

You were also missing the comma before SUM(amount) , the table name after FROM , and you need to qualify amount with the table name because both tables have a column named amount . 您还缺少在SUM(amount)之前的逗号,在FROM后面的表名,并且您需要使用表名来限定amount ,因为两个表都有一个名为amount的列。 You also had user.empnum in your ON clause, but that column is users.empid . 您的ON子句中也有user.empnum ,但该列是users.empid

You should select and group by the column from users , not fpo , because LEFT JOIN can return rows where the columns from fpo are NULL if there's no match for users.empid . 您应该选择并按users而不是fpo的列进行fpo ,因为如果users.empid不匹配,则LEFT JOIN可以返回fpo NULLusers.empid

This seems to work for me. 这似乎为我工作。 Thanks! 谢谢!

    result = mysqli_query($con,"select f.amount , u.empnum , u.amount , 
                                SUM(f.amount) AS total
                             FROM users AS u
                             LEFT JOIN fpo AS f ON f.empnum = u.empnum
                             GROUP BY u.empnum")

                              or die(mysqli_error($con));

while($row = mysqli_fetch_array($result)) {
      echo "Emp".$row['empnum']."----".$row['total']."----Amount----".$row['amount']."<br>";

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

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