简体   繁体   English

MySQL选择计数(从另一个表中)大于X的项目

[英]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.

相关问题 MySQL从表y的类别中选择,其中Count(来自另一个表的类别产品的计数)大于零 - MySQL Select from category in table y where Count (of the category's product from another table) is greater than zero Laravel 4-选择连接表中的行数大于0的行 - Laravel 4 - Select rows where count of rows in joined table greater than 0 从日期大于或等于今天的mysql表中选择记录 - Select record(s) from mysql table where date is greater than or equal to today 从一个表中选择计数,另一个表中有一个位置 - Select count from a table with a Where on another table 选择第一个大于x的值(在MySQL中) - Select first greater value than the x in MySQL MYSQL从表中选择并从另一个中计数 - MYSQL select from table and count from another 如果列是文本和数字,我如何从列值大于某个值的mysql表中选择 - how can i select from a mysql table where column value is greater than some value if the column is text and numbers MySQL选择具有两个条件的行,并且具有相同列ID的计数大于1 - MySQL select rows with two where conditions and count greater than 1 with same column ID MySQL 表中的 Select 行,其中 PHP 时间戳早于 X - Select rows from MySQL table where PHP timestamp is older than X 如果字段小于0且大于0,则MySQL select需要计数 - MySQL select need count if field less than 0 and greater than 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM