drsxobip501258 2014-10-03 03:27
浏览 50
已采纳

LEFT JOIN无法正常工作

I need some help with LEFT Join

first db is "fpo"

id----empnum-----amount---date----ponum

second db is "users"

id----amount-----empid----username----password

I need to total the "amount" in fpo for each user and compare the total to the amount from users.

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.

Here is what the data looks like in 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.

I need to do this for everyone in the users db. I have about 150 people in there right now.....

Thank you for your time and help!!! Probably a simple problem :(

  • 写回答

2条回答 默认 最新

  • dqb77047 2014-10-03 03:35
    关注

    You're not echoing the summed amount, $row['amount'] is the amount of a single row, you want $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. You also had user.empnum in your ON clause, but that column is 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?