dongpangbu4016 2014-01-21 20:06
浏览 66
已采纳

mysql内部加入和动态排名计算错误#1241

i am trying to calculate the rank of the user in my portal... i am joining user name from the user_table_name to the rank_table and calculating there rank dynamically if i run the query individually it runs fine but when i use it together there is a #1241 error here is 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  

Thanks Gordon for below query

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;

but the query gives me weird results such as if the 10 and 11 position user have same point then there rank is shows as 11 not 10..so can you please help

  • 写回答

1条回答 默认 最新

  • douguanya4248 2014-01-21 20:23
    关注

    A subquery to a select can only return one column. Not a bunch of them. I think you want something like this:

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的