我有这些表
用户{id,fname,lname}
class1 {id,user_id,l1,l2,l3 ...}
在l1 etc字段的类表中,它可以是Present / Absent / - 。
我想要一个php函数,它将选择所有用户,在class1表中找到它们,并计算它们存在的次数并存储它。
我知道它至少需要两个while循环。一个用户,一个用于班级。
我已经启动了用户,但不知道如何进行课程部分。
$user = mysqli_query($connection, "SELECT * FROM users ORDER BY fname");
while($fetch = mysqli_fetch_array($user))
{
$uid = $fetch['id'];
// Next Query and While Loop
}
答案 0 :(得分:0)
class1 (id, user_id, l_id, present)
表可能会受益于Present/Absent/-
的标准化,每个$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'];
}
值都有单独的行而不是列。
现在这应该有效:
{{1}}