1

I have two sql queries...

set  @count:=0;
select @count:=@count+1 as SNO, col1, col2 FROM table;

I want to combine above queries into a single query. Any Help?

Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59

4 Answers4

2

You can simply do this,

select @count:=@count+1 as SNO, col1, col2 
FROM table, (SELECT @count:=0) r ;

Just like adding RowNumber for each row

select @rownum:=@rownum+1 ‘rank’, 
       p.* 
from player p, (SELECT @rownum:=0) r 
order by score 
desc limit 10;

Adding RowNumber in MySQL

John Woo
  • 258,903
  • 69
  • 498
  • 492
2

As per my understanding,you are looking for Row_Number function in this case. If this is correct, please have a look here

e.g.

Select @count := @count + 1 As SNO, col1, col2
From table ,(SELECT @count:=0) foo

may help

Also you can refer ROW_NUMBER, Partition, and Over in MySQL for more understanding on the same

Community
  • 1
  • 1
Niladri Biswas
  • 4,153
  • 2
  • 17
  • 24
1

Combining two queries..

SELECT t1.field1, t1.field2, t2.field1
FROM (query1) as t1, (query2) as t2
WHERE t1.field1= t2.field1

Hope this will works ...

Basith
  • 1,077
  • 7
  • 22
0
select @count:=@count+1 as SNO, col1, col2 FROM table, (SELECT @count:=0) t;
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59