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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误