2014-05-15 64 views
0

嗨,這是我在mysql中使用的代碼,它的工作正常..是否有任何方法可以爲GRP聲明一個變量並在代碼中調用它。這樣我就可以再次使用的代碼,如果我必須做的比玻璃鋼和其他任何名稱等一些列的數學,因爲名字不斷改變所有的時間存儲過程使用參數調用特定的列

 SELECT GRP, 
    @_11:=power(GRP,0.1)+0.1*@_11 AS GRP_11 , 
    @_12:=power(GRP,0.2)+0.1*@_12 AS GRP_12 , 
    @_13:=power(GRP,0.3)+0.1*@_13 AS GRP_13 , 
    @_14:=power(GRP,0.4)+0.1*@_14 AS GRP_14 , 
    @_15:=power(GRP,0.5)+0.1*@_15 AS GRP_15 , 
    @_16:=power(GRP,0.6)+0.1*@_16 AS GRP_16 , 
    @_17:=power(GRP,0.7)+0.1*@_17 AS San_Orange_CS_GRP_17 , 
    @_18:=power(GRP,0.8)+0.1*@_18 AS GRP_18 , 
    @_19:=power(GRP,0.9)+0.1*@_19 AS GRP_19 , 
    from DUMMY JOIN (SELECT @_11:=0,@_12:=0,@_13:=0,@_14:=0,@_15:=0,@_16:=0, 
    @_17:=0,@_18:=0,@_19:=0) t; 

在此先感謝...

+0

您可以使用像'選擇@grp:= GRP爲GRP,@ _11:=功率(@grp, 0.1)+0.1 * @ _ 11 AS GRP_11,...' –

+0

感謝它的工作 – user3571489

+0

@Ravinder那麼AS GRP_11呢?有可能在那裏改變GRP嗎? – user3571489

回答

0

您可以使用內聯變量。

select @grp:=GRP as grp, @_11:=power(@grp,0.1)+0.1*@_11 AS GRP_11, ... 

更改您的如下查詢:

SELECT 
    @grp := GRP as grp, 
    @_11 := power(@grp, 0.1) + 0.1 * @_11 AS GRP_11 , 
    @_12 := power(@grp, 0.2) + 0.1 * @_12 AS GRP_12 , 
    @_13 := power(@grp, 0.3) + 0.1 * @_13 AS GRP_13 , 
    @_14 := power(@grp, 0.4) + 0.1 * @_14 AS GRP_14 , 
    @_15 := power(@grp, 0.5) + 0.1 * @_15 AS GRP_15 , 
    @_16 := power(@grp, 0.6) + 0.1 * @_16 AS GRP_16 , 
    @_17 := power(@grp, 0.7) + 0.1 * @_17 AS San_Orange_CS_GRP_17 , 
    @_18 := power(@grp, 0.8) + 0.1 * @_18 AS GRP_18 , 
    @_19 := power(@grp, 0.9) + 0.1 * @_19 AS GRP_19 , 
FROM DUMMY 
    JOIN ( 
      SELECT @_11:=0, @_12:=0, @_13:=0, @_14:=0, @_15:=0, 
        @_16:=0, @_17:=0, @_18:=0, @_19:=0 
     ) t; 
+0

是否有可能給........... AS @ grp_11,因爲我必須經常更改名稱... – user3571489

+0

這隻有在存儲過程中使用預準備語句時纔有可能。 –

+0

但是,如果查詢返回單個行將會很有意義。對於多行結果集,我們不能爲每行設置別名,這也是一個錯誤。 –

相關問題