组合两个mysql查询


Combining two mysql queries

我有两个sql查询。。。

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

我想将以上查询合并为一个查询。有什么帮助吗?

你可以简单地做到这一点,

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

就像为每行添加RowNumber一样

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

在MySQL中添加RowNumber

根据我的理解,在这种情况下,您正在寻找Row_Number函数。如果这是正确的,请看一下这里的

例如

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

可能有助于

您还可以参考MySQL中的ROW_NUMBER、Partition和Over,以获得对同一的更多理解

组合两个查询。。

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

希望这能奏效。。。

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