Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

Combining two mysql queries

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?

Upvotes: 1

Views: 161

Answers (4)

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

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

Upvotes: 0

John Woo
John Woo

Reputation: 263693

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

Upvotes: 2

Niladri Biswas
Niladri Biswas

Reputation: 4171

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

Upvotes: 2

Basith
Basith

Reputation: 1075

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 ...

Upvotes: 1

Related Questions