2013年11月1日 星期五

MYSQL 排名

參考來源:http://www.1keydata.com/tw/sql/sql-rank.html



SELECT a1.Name, a1.Sales, COUNT(a2.Sales) Sales_Rank 
FROM Total_Sales a1, Total_Sales a2 
WHERE a1.Sales < a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name) 
GROUP BY a1.Name, a1.Sales 
ORDER BY a1.Sales DESC, a1.Name DESC;


  # ------------------------------------------- 
 $sql="
 select a.term_class_student_sn, a.avg1, count(b.avg1) rank 
 from 
   (select sn as term_class_student_sn ,avg1
    from ".$xoopsDB->prefix("ugm_scroeqry_term_class_student")."  
    where term_class_sn='{$term_class_sn}' and enable='1') a, 
   
   (select sn as term_class_student_sn ,avg1
   from ".$xoopsDB->prefix("ugm_scroeqry_term_class_student")."  
   where term_class_sn='{$term_class_sn}' and enable='1') b 
   
   where a.avg1 < b.avg1 or (a.avg1=b.avg1 and a.term_class_student_sn = b.term_class_student_sn) 
   group by a.term_class_student_sn, a.avg1 
   order by a.avg1 desc, a.term_class_student_sn desc;
 ";die($sql);
  # ----http://www.1keydata.com/tw/sql/sql-rank.html     

沒有留言: