SQL语句错误:我想选择获胜最多的用户

时间:2015-09-01 06:32:02

标签: mysql

有两个表,用户和拍卖,我试图获得赢得最多胜利的用户的idfirstnamelastname在特定时间,胜利由具有值1的a.closed验证。

 SELECT a.winner_id AS 'Winners.id', u.first_name AS 'Winners first name', u.last_name AS 'Winners last name',
                            COUNT( * ) as count
                            FROM    auctions a
                            RIGHT JOIN users u ON a.winner_id = u.id
                            GROUP BY a.winner_id
                            ORDER BY a.winner_id
                            WHERE a.closed = '1' DESC AND
                            BETWEEN '".$dateFrom." 00:00:00' AND '".$dateTo." 23:59:59'
                            LIMIT    1 

不幸的是我收到了这个错误:

  #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE a.closed = '1' DESC AND BETWEEN '".$dateFrom." 00:00:00' AND '".$' at line 7

5 个答案:

答案 0 :(得分:1)

where子句附近的语法不正确。 DESCASC基本上与ORDER BY子句一起使用。

接下来,GROUP BY子句和ORDER BY子句位于where子句之后。您需要浏览MYSQL manual以详细查找语法。

所以你试试

SELECT a.winner_id AS 'Winners.id', u.first_name AS 'Winners first name', u.last_name AS 'Winners last name',
 COUNT( * ) as count
 FROM    auctions a
  RIGHT JOIN users u ON a.winner_id = u.id
   WHERE a.closed = '1' AND
   datecolumn BETWEEN  '00:00:00' AND  '23:59:59' --Change it as per your datecolumn name
   GROUP BY a.winner_id
   ORDER BY a.winner_id DESC 
   LIMIT    1 

答案 1 :(得分:0)

你的语法错了。必须是select .. from ... join .. where ..group by .. order by ...

SELECT a.winner_id AS 'Winners.id', u.first_name AS 'Winners first name', u.last_name AS 'Winners last name',
COUNT(*) as count
FROM  auctions a
RIGHT JOIN users u ON a.winner_id = u.id
WHERE a.closed = '1' AND
BETWEEN '".$dateFrom." 00:00:00' AND '".$dateTo." 23:59:59'
GROUP BY a.winner_id
ORDER BY a.winner_id DESC 
LIMIT 1 

答案 2 :(得分:0)

您可以在查询结束时进行订购,对于desc,您需要首先使用某个列的订单。

您可以尝试以下 -

SELECT a.winner_id AS 'Winners.id', u.first_name AS 'Winners first name', u.last_name AS 'Winners last name',
                            COUNT( * ) as count
                            FROM    auctions a
                            RIGHT JOIN users u ON a.winner_id = u.id
                            WHERE a.closed = '1' 
                            and BETWEEN '".$dateFrom." 00:00:00' AND '".$dateTo." 23:59:59'
                            group by a.winner_id 
                            order by a.winner_id desc LIMIT    1 

答案 3 :(得分:0)

WHERE ...必须位于select语句中的GROUP BY ...之前。

尝试更改此内容。

请参阅:https://dev.mysql.com/doc/refman/5.6/en/select.html

如果您需要选择并排序然后从中进行选择,则应使用子查询。

见这里:https://dev.mysql.com/doc/refman/5.6/en/from-clause-subqueries.html

答案 4 :(得分:0)

GOT IT谢谢大家。      选择a.winner_id AS'用户ID',u.first_name AS'名字',u.last_name AS'姓氏',COUNT()AS '赢得的拍卖数量'                                 来自拍卖a                                 RIGHT JOIN用户u on a.winner_id = u.id                                 a.closed =' 1'和a.end_time                                 BETWEEN'"。$ dateFrom。" 00:00:00' AND'"。$ dateTo。" 23:59:59'                                 GROUP BY a.winner_id                                 按计数排序()DESC                                 限制1