订购周数和分别。 mysql中的数据以51开头,以第12周结束

时间:2015-04-27 09:40:29

标签: mysql

我有这个查询按周数显示snowdepth。除了wk,1,2 3等之外,表中没有其他日期(ish)注册。

SELECT 
  res_id,
  wk,
  ROUND(templo, 0) AS templo,
  ROUND(temphi, 0) AS temphi,
  ROUND(snow_valley, 0) AS snow_valley,
  ROUND(snow_mountain, 0) AS snow_mountain 
FROM
  sv_cond_vw_wk 
GROUP BY res_id,
  wk 
HAVING (res_id = 6) 
  AND (wk = 51 
    OR wk = 52 
    OR wk = 52 
    OR wk < 13)

在第12周之后输出第51周和第52周但我希望在第1周之前得到它: 51,52,1,2,3,4

1 个答案:

答案 0 :(得分:1)

您可以在FIELD(wk,52,51) DESC子句中使用ORDER BY,以便wk =&gt;首先出现51然后wk =&gt; 52然后休息

SELECT 
  res_id,
  wk,
  ROUND(templo, 0) AS templo,
  ROUND(temphi, 0) AS temphi,
  ROUND(snow_valley, 0) AS snow_valley,
  ROUND(snow_mountain, 0) AS snow_mountain 
FROM
  sv_cond_vw_wk 
GROUP BY res_id,
  wk 
HAVING (res_id = 6) 
  AND (wk = 51 
    OR wk = 52  
    OR wk < 13)
ORDER BY FIELD(wk,52,51) DESC , wk