在一个查询中组合不同的排名

时间:2016-03-29 19:43:20

标签: php mysql sql

我需要将不同的排名查询合并到一个简单的查询中,并且只查询性能问题。我尝试了总结和条款,但所有这些都不适合我。

$kacinci = mysql_query("SELECT ranking FROM
(SELECT 
@rank:=case when bakiye+0 =@s then @rank else @rank+1 end AS ranking,
@s:=bakiye+0, ID
FROM uye , (SELECT @rank:=0,@s:=0) AS i
ORDER BY bakiye+0 DESC
) new_alias WHERE ID='$profilid'");
$kacincix = mysql_fetch_array($kacinci);
$kacincix = $kacincix[0];

//kaç katına hafta
$kackatinahafta = mysql_query("SELECT ranking FROM
(SELECT 
@rank:=case when kackatinahafta =@s then @rank else @rank+1 end AS ranking,
@s:=kackatinahafta, ID
FROM uye , (SELECT @rank:=0,@s:=0) AS i
ORDER BY kackatinahafta DESC
) new_alias WHERE ID='$profilid'");
$kackatinahaftax = mysql_fetch_array($kackatinahafta);
$kackatinahaftax = $kackatinahaftax[0];

//kaç para hafta

$nekadarhafta = mysql_query("SELECT ranking FROM
(SELECT 
@rank:=case when nekadarhafta =@s then @rank else @rank+1 end AS ranking,
@s:=nekadarhafta, ID
FROM uye , (SELECT @rank:=0,@s:=0) AS i
ORDER BY nekadarhafta DESC
) new_alias WHERE ID='$profilid'");
$nekadarhaftax = mysql_fetch_array($nekadarhafta);
$nekadarhaftax = $nekadarhaftax[0];

//kaç katına ay
$kackatinaay = mysql_query("SELECT ranking FROM
(SELECT 
@rank:=case when kackatinaay =@s then @rank else @rank+1 end AS ranking,
@s:=kackatinaay, ID
FROM uye , (SELECT @rank:=0,@s:=0) AS i
ORDER BY kackatinaay DESC
) new_alias WHERE ID='$profilid'");
$kackatinaayx = mysql_fetch_array($kackatinaay);
$kackatinaayx = $kackatinaayx[0];

//kaç para ay

$nekadaray = mysql_query("SELECT ranking FROM
(SELECT 
@rank:=case when nekadaray =@s then @rank else @rank+1 end AS ranking,
@s:=nekadaray, ID
FROM uye , (SELECT @rank:=0,@s:=0) AS i
ORDER BY nekadaray DESC
) new_alias WHERE ID='$profilid'");
$nekadarayx = mysql_fetch_array($nekadaray);
$nekadarayx = $nekadarayx[0];

注意:我知道mysql_query已被弃用,但项目是基于该功能构建的,并计划很快更新。

1 个答案:

答案 0 :(得分:0)

尝试此查询:

  SELECT ID
, bakiye
, bakiye_rank
, kackatinahafta
, kackatinahafta_rank
, nekadarhafta
, nekadarhafta_rank
, kackatinaay
, kackatinaay_rank
, nekadarayx
, nekadarayx_rank
FROM uye 
--you can rank over count(field value), for example bakiye
join (SELECT bakiye, count(bakiye) c, case when @c1 = count(bakiye) then @r1 when (@r1 := @r1 + 1) and (@c1 := count(bakiye)) then @r1 end AS bakiye_rank 
  from uye, (SELECT @r1 := 0, @c1 := 0) r group by bakiye order by 2 desc) AS r1 on uye.bakiye = r1.bakiye
--you can rank over count(field value) and field value, for example kackatinahafta
join (SELECT kackatinahafta, count(kackatinahafta) c, case when @c2 = kackatinahafta then @r2 when (@r2 := @r2 + 1) and (@c2 := kackatinahafta) then @r2 end AS kackatinahafta_rank 
  from uye, (SELECT @r2 := 0, @c2 := 0) r group by kackatinahafta order by 2 desc, 1 desc) AS r2 on uye.kackatinahafta = r2.kackatinahafta
--you can rank over field value, for example nekadarhafta
join (SELECT nekadarhafta, case when @c3 = nekadarhafta then @r3 when (@r3 := @r3 + 1) and (@c3 := nekadarhafta) then @r3 end AS nekadarhafta_rank 
  from uye, (SELECT @r3 := 0, @c3 := 0) r group by nekadarhafta order by 1 desc) AS r3 on uye.nekadarhafta = r3.nekadarhafta
join (SELECT kackatinaay, case when @c4 = kackatinaay then @r4 when (@r4 := @r4 + 1) and (@c4 := kackatinaay) then @r4 end AS kackatinaay_rank 
  from uye, (SELECT @r4 := 0, @c4 := 0) r group by kackatinaay order by 1 desc) AS r4 on uye.kackatinaay = r4.kackatinaay
join (SELECT nekadarayx, case when @c5 = nekadarayx then @r5 when (@r5 := @r5 + 1) and (@c5 := nekadarayx) then @r5 end AS nekadarayx_rank 
  from uye, (SELECT @r5 := 0, @c5 := 0) r group by nekadarayx order by 1 desc) AS r5 on uye.nekadarayx = r5.nekadarayx
WHERE ID=$profilid

编辑: 纠正,添加()... - Operator Precedence 这:@ r2:= @ r2 + 1和@ c2:= kackatinahafta
计算如下:@ r2:=(@ r2 + 1和@ c2:= kackatinahafta),
但应该是:(@ r2:= @ r2 + 1)和(@ c2:= kackatinahafta)