结合两个mysql查询

时间:2012-09-05 05:27:19

标签: php mysql sql

我有两个SQL查询...

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

我想将上述查询合并到一个查询中。任何帮助?

4 个答案:

答案 0 :(得分:2)

你可以这么做,

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;

<强> Adding RowNumber in MySQL

答案 1 :(得分:2)

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

e.g。

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

可能有帮助

另外,您可以参考ROW_NUMBER, Partition, and Over in MySQL以获得对相同

的更多理解

答案 2 :(得分:1)

合并两个查询..

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

希望这会有效......

答案 3 :(得分:0)

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