Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

row number - How to get RowNumber() with Partition in MYSQL

RowNumber() with Partition in MYSQL

i want the below output based on id-Foreign key

 id | Name | rownumber
 1     a      1
 1     b      2
 1     ads    3    
 2    dsfs    1  
 2    sadf    2
 2    sdfsa   3
 2    dfsfs   4 
 3     dsf    1
 3     adsf   2
 3     sdd    3 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I barely understood what you mean. There's no RowNumber() function in mysql, and partitioning has nothing to do with your request.

It's:

SELECT 
  t.*, 
  @cur:= IF(id=@id, @cur+1, 1) AS RowNumber, 
  @id := id 
FROM 
  t CROSS JOIN 
    (SELECT @id:=(SELECT MIN(id) FROM t), @cur:=0) AS init 
ORDER BY 
  t.id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...