1

我需要放在set @rank:=0;这个查询中,但我可以把它放在哪里?

SELECT @rank:=@rank+1 AS rank, p.* FROM points p 
inner join distributor d 
on p.distributor_id=d.id_distributor 
where p.month='$prev_month' 
and d.group='$dist_group' 
ORDER BY p.tot_point DESC

我必须mysql_query("set @rank:=0;");在主查询之前使用,它的工作原理。但在另一台服务器上它不会工作。

有任何想法吗 ?

4

1 回答 1

2
SELECT @rank:=@rank+1 AS rank, p.* FROM points p, (SELECT @rank:=0) AS dummy
inner join distributor d 
on p.distributor_id=d.id_distributor 
where p.month='$prev_month' 
and d.group='$dist_group' 
ORDER BY p.tot_point DESC

基本上只是, (SELECT @rank:=0) AS dummyFROM points p.

我对此查询的首选布局:

SELECT @rank := @rank + 1 AS rank, p.*
FROM 
    points AS p
  CROSS JOIN 
    (SELECT @rank:=0) AS dummy
  INNER JOIN 
    distributor AS d
      ON p.distributor_id = d.id_distributor 
WHERE p.month = '$prev_month' 
  AND d.group='$dist_group'
ORDER BY p.tot_point DESC ;
于 2012-12-18T12:30:32.177 回答