douqi3913 2016-06-10 13:52
浏览 23
已采纳

计算单词的所有实例并存储在变量中

I have these tables

users {id,fname,lname}

class1 {id,user_id,l1,l2,l3...}

In the class table in the l1 etc fields it can either be Present/Absent/-.

I want a php function that will select all users, find them in the class1 table, and count how many times they are present and store this.

I know its going to need at least two while loops. One for the users and one for the class.

I have started the users, but dont know how to do the class section.

    $user = mysqli_query($connection, "SELECT * FROM users ORDER BY fname");

      while($fetch = mysqli_fetch_array($user))
      {

    $uid = $fetch['id'];

    // Next Query and While Loop

}
  • 写回答

1条回答 默认 最新

  • doubo4336 2016-06-10 15:46
    关注

    class1 table might benefit from normalization into class1 (id, user_id, l_id, present) with separate row rather than column for each Present/Absent/- value. As it is now this should work:

    $user = mysqli_query($connection, "
        Select *
          , (Select Count(*) From class1 Where user_id = users.id AND l1 = 'Present')
          + (Select Count(*) From class1 Where user_id = users.id AND l2 = 'Present') 
          + (Select Count(*) From class1 Where user_id = users.id AND l3 = 'Present')
          /* ... and so on for each l ... */
            As present
        From users
    ");
    
    while($fetch = mysqli_fetch_array($user))
    {
        $uid = $fetch['id'];
        $present = $fetch['present'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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