我有两个表,用户和hra。用户在用户表中只能有一条记录,但他可以有多个hras。我想要做的只是每个用户检索一个hra。我有以下查询。任何人都可以帮忙做到这一点?
$query = "SELECT u.id AS user_id, h.id AS hra_id FROM users u
INNER JOIN hra h ON u.id = h.user_id
WHERE h.date BETWEEN ? AND ?
AND h.done = 1
答案 0 :(得分:2)
假设你想要MAX(h.id),这应该有效:
SELECT u.id AS user_id, MAX(h.id) AS hra_id FROM users u
INNER JOIN hra h ON u.id = h.user_id
WHERE h.date BETWEEN ? AND ?
AND h.done = 1
GROUP BY u.id