Ahmad Qasim
Ahmad Qasim

Reputation: 474

ROW_NUMBER() equivalent function in mysql

Hey every one I want to generate surrogate key by getting Max() from one table in sql I did some thing like that

coalesce(max(INDVL_ID), 0) + ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

now I have to do same thing in mysql so issue is that I am unable to get alternate of ROW_NUMBER() Any one have solution

Upvotes: 0

Views: 1106

Answers (2)

Nedder
Nedder

Reputation: 68

The answer here has been depreciated. Please use the following in MySQL version 8:

ROW_NUMBER() OVER ( ORDER BY someField) AS 'row_number'

EXAMPLE:

SELECT 
    ROW_NUMBER() OVER (ORDER BY s.Id) AS 'row_num', 
    s.product,
    s.title
FROM supplies AS S

Upvotes: 0

Lkbhai Lr
Lkbhai Lr

Reputation: 264

try this

SELECT @a:=@a+1 rownum,t.* FROM table t,(select @a:=0)v;

Upvotes: 2

Related Questions