Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 无法在case when语句中放置子查询条件_Mysql_Sql - Fatal编程技术网

Mysql 无法在case when语句中放置子查询条件

Mysql 无法在case when语句中放置子查询条件,mysql,sql,Mysql,Sql,我有一张桌子,一个座位,需要根据给定的条件打印一些数据。我无法执行查询,因为语法错误从来都不是描述性的,我无法推断其背后的原因 这是生成语法错误的查询: select (case id when mod(id, 2)=0 then id=id+1 when mod(id,2) <> 0 and id != (select count(*) from seat) then id=id-1 else id=id) as

我有一张桌子,一个座位,需要根据给定的条件打印一些数据。我无法执行查询,因为语法错误从来都不是描述性的,我无法推断其背后的原因

这是生成语法错误的查询:

select (case id when mod(id, 2)=0 then id=id+1 
                when mod(id,2) <> 0 and id != (select count(*) from seat) then id=id-1 
                else id=id) as id, 
       student 
from seat;
错误是:

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解第2行“as id,student from seat”附近使用的正确语法


任务是切换相邻行的特定列的值。完整问题可用。

您不需要在案例表达式的值中指定输出变量。此外,当您在when条件中使用逻辑表达式时,不应指定案例的输入,并且您缺少案例的结尾。请尝试以下方法:

select case when mod(id, 2) =  0 then id-1 
            when mod(id, 2) <> 0 and id != (select count(*) from seat) then id+1
            else id
       end as id, 
       student 
from seat
order by id;

那么为什么是0呢?不应该改为id+1吗?@AviralSrivastava我刚从你的表达式中复制了值……哦,对不起,我正在更新它。我的错。@AviralSrivastava注意,您也不应该将id作为case表达式的输入;只有在将id值与固定值进行比较时,您才会这样做。例如,案例id为2时x时3时y…@AviralSrivastava我为您添加了一个工作演示。
id  student
1   Doris
2   Abbot
3   Green
4   Emerson
5   Jeames