mysql内部加入&动态等级计算错误#1241

时间:2014-01-21 20:06:55

标签: php mysql

我正在尝试计算门户网站中用户的排名...我正在加入user_table_namerank_table的用户名,并在我单独运行查询时动态计算排名运行正常,但当我一起使用时,有一个#1241错误 这是mycode

SELECT (SELECT utn.name, rt.amount_left, rt.loan_amount, rt.currency_amount,
               rt.stock_amount, rt.fd_amount, rt.user_money
        FROM `user_table_name` AS utn INNER JOIN
             `rank_table` AS rt on rt.user_id=utn.userid
       ) AS ut, 
       (SELECT  COUNT(*)
        FROM    rank_table ui
        WHERE   (ui.amount_left) >= (uo.amount_left)
       ) AS rank 
FROM    rank_table uo
WHERE   1
ORDER BY rank ASC  

感谢Gordon提供以下查询

SELECT utn.name, rt.amount_left, rt.loan_amount, rt.currency_amount,
   rt.stock_amount, rt.fd_amount, rt.user_money,
   (SELECT  COUNT(*)
     FROM    rank_table ui
     WHERE   (ui.amount_left) >= (rt.amount_left)
   ) AS rank
FROM `user_table_name` utn INNER JOIN
 `rank_table` rt
  on rt.user_id = utn.userid
ORDER BY rank ASC;

但 查询给了我奇怪的结果,如果10和11位置用户有相同的点,那么排名显示为11而不是10 ..那么你可以请帮助

1 个答案:

答案 0 :(得分:2)

select的子查询只能返回一列。不是一堆。我想你想要这样的东西:

SELECT utn.name, rt.amount_left, rt.loan_amount, rt.currency_amount,
       rt.stock_amount, rt.fd_amount, rt.user_money,
       (SELECT  COUNT(*)
         FROM    rank_table ui
         WHERE   (ui.amount_left) >= (rt.amount_left)
       ) AS rank
FROM `user_table_name` utn INNER JOIN
     `rank_table` rt
      on rt.user_id = utn.userid
ORDER BY rank ASC;