doumao1887 2014-09-29 18:44
浏览 208

两个mysql表和每个循环之间的关联数组

Hey Guys i need some help with Associative array

Here is what i have.

I have two mysql table one called users...Setup like this.

[ MYSQL DB users]
***************************************
Id------name-------empnum------amount
1 ------testa------123456------40.00
2 ------testb------652526------300.00
3 ------Testc------919825------100.00
4 ------Testd------354694------50.00

And the other db is setup like this.

[ MYSQL DB po ]
***************************************
Id------ponum------reason----empnum---------total------reason
1 ------1234------testa------123456---------40.00------Demo
2 ------1235------testb------652526--------300.00------Demo
3 ------1236------Testc------919825--------100.00------other
4 ------1237------Testd------123456---------50.00------Demo

What i need to do is select each user from "Users" db and then find them in the "Po" db

Then i need to add up the total they have spent (in PO) and compare it with Amount from users db.

I have this javascript bar graph that i would like to display next to each persons name to show them how close to 100% they have spent.

<div id="progressBar<? echo $id; ?>" class="default"><div></div></div>
    <script>
        progressBar(<?php echo round(($sum/$total) * 100); ?>, $('#progressBar<? echo $id; ?>'));
    </script>  

My question is How do i go about this. My first thought was to use Associative array and get amount and empnum from users and then pull that info from mysql and then compare that with total....Some where between point a and b i get lost. I dont have a great understanding on how A-array work with mysql and two tables. from what i have found it looks like i will have to use a for each loop as well.?? Am i going about this the wrong way.. Is there an easier way???

  • 写回答

1条回答 默认 最新

  • dpzo13732 2014-09-29 19:00
    关注

    You can do most of it in a single query:

    SELECT u.amount, SUM(p.total) 'po_total'
    FROM users u LEFT JOIN po p
      ON u.empnum = p.empnum
    GROUP BY p.empnum
    

    And then:

    $percentage = $row['po_total'] / $row['amount'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?