我必须使用数据库表:"用户"和"结果"。我想从表用户浏览用户并计算表结果中每个用户的特定记录(计算所玩游戏和获胜)。我的问题:
$q = "SELECT *, (SELECT count(*)
FROM users, results
WHERE (results.u1 = users.id OR results.u2 = users.id)
AND (results.p1 > 0 OR results.p2 > 0)
AND gid = '$gid' AND users.gender = '$gender') as gp
FROM users LEFT JOIN results
ON users.id = results.u1
||
users.id = results.u2
WHERE gid = '$gid' AND users.gender = '$gender'
GROUP BY users.id LIMIT 0, 9";
这确实会返回9个用户,但" gp"显示所有记录的计数,但我希望gp部分只计算循环中每个用户的记录和ORDER。
你可以帮我解决这个问题吗?谢谢你!
答案 0 :(得分:0)
选择用户ID并创建其别名并在子查询中使用它
select user_id as userid (select count(*) from result where results.u2 = userid) as result_count from users ;