我有两张桌子:
user messages
+---------+ +----------------+
|id|credit| |id|user_id| ... |
+---------+ +----------------+
|01| 05 | |01| 01 | ... |
|02| 01 | |02| 01 | ... |
|03| 00 | |03| 01 | ... |
+---------+ |04| 02 | ... |
|05| 02 | ... |
|06| 03 | ... |
+----------------+
我需要从消息中选择 n 消息,其中 n = user.credit 。我想执行查询的查询,如:
SELECT *
FROM user u JOIN messages m ON u.id = m.user_id LIMIT u.credit
WHERE user in ...;
并且应该返回01,02,03,04行但不返回05,06。
我知道我不能写LIMIT u.credit
但是想要这种行为,根据他们拥有的信用值来限制为每个用户提取的消息数量(用户01 whit限制为5,用户02为限制为1等等)我希望一次性获取所有消息,以避免为每个用户执行COUNT然后选择SELECT。
我正在使用带有InnoDB的MySQL
答案 0 :(得分:4)
这是普通SQL中的解决方案,不使用变量:
select user.*, messages.*
from user inner join messages
on user.id=messages.user_id
where (select count(*) from messages m
where m.user_id=user.id
and m.id<messages.id)<user.credit
答案 1 :(得分:1)
我担心这会很慢:
SELECT u.*
, m.*
FROM
user AS u
JOIN
( SELECT m1.*, COUNT(*) AS cnt
FROM messages m1
JOIN messages m2
ON m2.user_id = m1.user_id
AND m2.id <= m1.id
GROUP BY m1.user_id, m1.id
) m
ON m.user_id = u.id
AND m.cnt <= u.credit ;
答案 2 :(得分:0)
如果你决定在SQL中有一种方法可以做到这一点。
SELECT u.*
FROM user u JOIN messages m ON u.id = m.user_id
inner join user u2 on u2.id <= u.id
WHERE user in ...
GROUP BY u.id
HAVING count(u2.id) < u.credit