1. 程式人生 > >ORACLE ROW_NUMBER () 函式轉成MySQL

ORACLE ROW_NUMBER () 函式轉成MySQL


 copy
  1. ORACLE  :
  2.  SELECT b.* ,  ROW_NUMBER () OVER ( PARTITION BY customer_name, dealer_code ORDER BY customer_no DESC) rank   FROM  tm_customer
  3. MYSQL:
  4. SELECT b.*, IF(@pdept = CONCAT(b.customer_name, b.dealer_code),@rank := @rank + 1,@rank := 1) AS rank,
  5. @pdept := CONCAT(b.customer_name, b.dealer_code),
  6. @rownum := @rownum + 1
  7. FROM
  8. (SELECT  b.* FROM tm_customer b ORDER BY CONCAT(b.customer_name, b.dealer_code) ASC, customer_no DESC) b, 
  9. (SELECT @rownum := 0,@pdept := NULL,@rank := 0) a