我有以下查询,该查询完全按预期运行(感谢stackoverflow的帮助人员)。但是,我意识到,除了使用该计数来验证消息是否小于等于3之外,我还想获取结果中返回的每一行的实际数字/计数。
这是为了让我可以根据每个返回的用户收到的消息数量自定义逻辑。
我尝试了几种不同的选择,但效果不佳。
SELECT u.*
FROM users u
WHERE
NOT EXISTS (
SELECT 1
FROM events e
WHERE e.user_id = u.id AND e.type = 'collection'
)
AND (
SELECT COUNT(*)
FROM messages m
WHERE m.user_id = u.id AND m.message_type = 'collection_reminder'
) <= 3
AND u.admin IS NULL
答案 0 :(得分:2)
将该子查询移动到选择列表。将条件放在having
子句的末尾(MySQL特殊技巧)。
SELECT u.*,
(
SELECT COUNT(*)
FROM messages m
WHERE m.user_id = u.id AND m.message_type = 'collection_reminder'
) as cnt
FROM users u
WHERE
NOT EXISTS (
SELECT 1
FROM events e
WHERE e.user_id = u.id AND e.type = 'collection'
)
AND u.admin IS NULL
HAVING cnt <= 3
答案 1 :(得分:0)
我对原始问题的回答略有修改:
<div id="jobb">
<form id="gombok">
<span>
<input type="button" name="previous" value="Previous">
<input type="button" class="toggle" href="#bal" name="next" value="Next">
</span>
</form>
</div>
<div id="bal" class="sib">
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<p>Paragraph 4</p>
</div>