修复通过交替1,2,3 - 1,2,3 - 1,2,3来命令MYSQL结果的查询

时间:2012-08-15 13:46:49

标签: mysql

之前我问了一个关于通过交替(1,2,3 - 1,2,3 - 1,2,3)行来排序MYSQL结果的问题,我发现了这个answer

我尝试将代码应用到我的情况中,这就是我所拥有的:

SELECT 
x.id, 
x.pic0,
x.bio,
x.bio2,
x.site,
x.url,
x.applet
FROM 
(SELECT 
t.id, 
t.pic0,
t.bio,
t.bio2,
t.site,
t.url,
t.applet
CASE
WHEN @site != t.site THEN @rownum := 0
WHEN @site = t.site THEN @rownum := @rownum + 1
ELSE @rownum
END AS rank,
@site := t.site
FROM ".$table." t,
(SELECT @rownum := 0, @site ORDER BY t.site) r) x 
ORDER BY x.rank, x.site

这是我得到的MYSQL错误 - 服务器正在使用MYSQL 5.1

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE WHEN @site != t.site THEN @rownum := 0 WHEN @site = t.site THEN @rownum :' at line 18
  1. 我是一个全新的和这个查询,我甚至无法理解它:)所以我对它有什么问题没有丝毫的想法。

  2. 如何在此查询中添加WHERE条件?

    WHERE bio LIKE'value'等等

1 个答案:

答案 0 :(得分:0)

您错过了,

t.applet , CASE WHEN @site ...
         ^................................here

在此处添加where

FROM ".$table." t,
(SELECT @rownum := 0, @site ORDER BY t.site) r) x 

WHERE bio LIKE 'value'

ORDER BY x.rank, x.site