[英]MySQL Select Items where Count (from another table) is greater than X
The following situation: I have a list of users and outfits, each user can create N outfits. 以下情况:我有一个用户和服装列表,每个用户可以创建N个服装。 Users can vote for their outfit.
用户可以投票给他们的衣服。
Now I want to list all outfits and users. 现在,我想列出所有服装和用户。 This works fine.
这很好。 But if I want to list all outfits of each each / all users WHERE the amount of required votes is reached.
但是,如果我想列出每个/所有用户的所有服装,则达到所需的票数。 So WHERE count_votes > required_votes.
因此,count_votes> required_votes。
Thats what I've got but it obviously gives me an error: 那就是我所拥有的,但显然给我一个错误:
SELECT ay_users.*,
ay_users_outfits.*,
COUNT(ay_votes.voteId) AS countvotes
FROM ay_users_outfits,
ay_users,
ay_votes
WHERE ay_users_outfits.outfitUserId = ay_users.userId
AND ay_votes.voteOutfitId = ay_users_outfits.outfitId
AND ay_users_outfits.outfitRequiredVotes <= countvotes
ORDER BY ay_users_outfits.outfitCreationDate ASC
#1054 - Unknown column 'countvotes' in 'where clause'
What am I doing wrong? 我究竟做错了什么?
Take a look at this: http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html 看一下这个: http : //dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html
You will need to use a HAVING clause, something like 您将需要使用HAVING子句,例如
SELECT
ay_users . *,
ay_users_outfits . *,
FROM
ay_users_outfits,
ay_users,
ay_votes
WHERE
ay_users_outfits.outfitUserId = ay_users.userId AND
ay_votes.voteOutfitId = ay_users_outfits.outfitId
GROUP BY ay_users.userId
HAVING ay_users_outfits.outfitRequiredVotes <= COUNT(ay_votes.voteId)
ORDER BY ay_users_outfits.outfitCreationDate ASC
The reason is that no column countvotes
exists and you are using it in your where clause... 原因是不存在任何列
countvotes
,而您正在where子句中使用它。
you will have to use COUNT(ay_votes.voteId)
in your where clause. 您将必须在where子句中使用
COUNT(ay_votes.voteId)
。
AND ay_users_outfits.outfitRequiredVotes <= COUNT(ay_votes.voteId)
You cannot use (named) calculations in your where clause 您不能在where子句中使用(命名)计算
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.