0

How to use Sql query for generating S.no from 1. I can't get the answer correctly

SELECT (@cnt := @cnt + 1) AS 'S.No',`barcode`,
(SELECT @cnt := 0) AS dummy
FROM  wp_weblib_outitems
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
john willson
  • 1
  • 1
  • 4
  • possible duplicate of [Generate serial number in mysql query](http://stackoverflow.com/questions/11094466/generate-serial-number-in-mysql-query) – ManojRK Sep 21 '15 at 05:46

3 Answers3

0

Try this:

SELECT  @s:=@s+1 AS 'S.No',`barcode`
FROM    wp_weblib_outitems,
        (SELECT @s:= 0) AS s;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

This is my query. i just want S.No as asc . How can i write this query as ascending order

SELECT DISTINCT (subject),a.title, a.callnumber, 
    a.author, a.recv_date,a.pubdate ,b.book_access_number,(@cnt := @cnt + 1) AS 'S.No'
    from wp_weblib_collection a
    inner join wp_weblib_book_log_reports b on a.barcode = b.book_access_number
    $student
    CROSS JOIN (SELECT @cnt := 0) AS dummy
    GROUP BY subject asc 
Mohit S
  • 13,723
  • 6
  • 34
  • 69
john willson
  • 1
  • 1
  • 4
0

You can try with ROW_NUMBER() as given below.......

SELECT ROW_NUMBER() OVER (ORDER BY [Column1]) AS 'NO',[Column1],[column2],.... FROM tblUser;
Priyank_Vadi
  • 1,028
  • 10
  • 27