一道SQL查询语句的问题(问题比较长,见问题补充),请各位高人帮帮忙,谢谢!

来源:百度知道 编辑:UC知道 时间:2024/06/28 17:35:51
有一张Table表,有一个字段ID(主键),是由1开始递增的。当前表中有5行
1
2
3
4
5
要求查询结果为
1 2
3 4
5 *
其中*为NULL(若表中有6行则*为6),跪求查询语句。无限感激!

要将int转换成varchar是不行的,所以这里我将*改成0来显示

select a.id,id2 = case
when b.id is null then 0
when b.id is not null then b.id
end
from
(select id from idd where id % 2 = 1) a
left join
(select id from idd where id % 2 = 0) b
on a.id+1=b.id

主键不能为空`!