我正在運行以下查詢以查找用戶的最佳連勝記錄。通過在mysql中的順序選擇大小寫
$sql="select sub.user_id as user_id,max(sub.streak) as streak,max(sub.units) as units
from
(
select
case when @x is null then @x:=user_id end,
case
when awarded_unit>0 and @x=user_id then @y:[email protected]+1
when awarded_unit<0 and @x=user_id then @y:=0
when awarded_unit>0 and @x<>user_id then @y:=1
when awarded_unit<0 and @x<>user_id then @y:=0
end as streak,
case
when awarded_unit>0 and @x=user_id then @z:[email protected]+awarded_unit
when awarded_unit<0 and @x=user_id then @z:=0
when awarded_unit>0 and @x<>user_id then @z:=awarded_unit
when awarded_unit<0 and @x<>user_id then @z:=0
end as units,
@x:=user_id as user_id,
awarded_unit
from $select_user_events_fights as u,$events as e,$event_fight_table as ef,$post_meta as pm where e.ID=ef.event_id
and ef.event_fight_id=u.event_fight_id and e.post_type='bt_events' and pm.post_id = e.ID and e.post_status ='publish' and pm.meta_key='_event_dt_time' and u.`awarded_unit`!=0 and u.season_id=$season_id and u.user_id=".$user->ID."
order by pm.meta_value desc,ef.fight_order desc
) as sub
group by sub.user_id";
mysql_query("set @y=0;");
mysql_query("set @x=null;");
mysql_query("set @z=0;");
如果我把訂單通過u.primary_key它工作正常。但是我希望事件按日期排列並按照順序排序。它在這種情況下會給出錯誤的結果。
我已經檢查了內部查詢語句順序。
通過語句排序("order by pm.meta_value desc,ef.fight_order desc"
),排序結果 經過計算最好的條紋,因此它給出了錯誤的結果。
請解釋如何得到正確的答案和我所缺少的here.Thanks
請不要使用'mysql_ *'函數編寫新代碼。他們不再被維護,並且社區已經開始[棄用流程](http://goo.gl/q0gwD)。請參閱 [紅盒子](http://goo.gl/OWwr2)?相反,您應該瞭解[準備好的 聲明](http://goo.gl/orrj0)並使用[PDO](http://goo.gl/TD3xh)或[MySQLi](http://in3.php達網絡/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/YXyWL)會幫助你。 如果你選擇PDO,[這裏是很好的教程](http://goo.gl/b2ATO)。 另請參閱[爲什麼我不應該在PHP中使用mysql函數?](http://goo.gl/J5jAo) –
好的,我將學習PDO,但它與我開發的一些舊代碼有關。感謝您的建議 –