用一条语句可以解决吗?我用两条就会
16 个解决方案
#1
select id,case when id%2=0 then grade+10 else grade end as grade
from tablename
from tablename
#2
select id,case grade%2 when 0 then grade+10 else grade end as grade from stu
#3
SELECT id ,
grade + CASE WHEN grade % 2 = 0 THEN 10
ELSE 0
END AS grade
FROM stu
#4
select id,grade=case when grade%2=0 then grade+10 else grade end
from tb
#5
这是什么原因呢?看图
#6
如果查询的参数除了ID,GRADE,还有很多个,能不能slect *, grade + CASE WHEN grade % 2 = 0 THEN 10 ELSE 0 END AS grade
FROM stu
#7
但是我上面的写法,好像查询了两次grad,有没有办法只查询一次
#8
select * 默认查询的是显示全部,请问如何用sql语句在select * 中隐藏显示字段为id这一列
#9
木有办法
只能手动把所有其他列写上去。
#10
+1
#11
如果列实在很多,楼主可以考虑拼sql的方式。
if OBJECT_ID('A') is not null drop table A
create table A(id int,col1 nvarchar(1),col2 nvarchar(1),col3 nvarchar(1),col4 nvarchar(1),col5 nvarchar(1))
declare @columnList varchar(2000) --字段列集合字符串
select @columnList=isnull(@columnList+',','')+name from syscolumns where id=OBJECT_ID('A') and name<>'id'
--输出列名
print @columnList --拿着去拼查询语句
drop table A
#12
噢噢,谢谢
#13
不是说ID为偶数的加10分么,为嘛都是Grade%2????
#14
select id,case when id%2=0 then grade+10 else grade end as grade from yourtable
#15
进来拜一下楼主的结贴率
#16
select id,grade+case when grade%2=0
then 10
else 0
end as grade
from table_name;
then 10
else 0
end as grade
from table_name;
#1
select id,case when id%2=0 then grade+10 else grade end as grade
from tablename
from tablename
#2
select id,case grade%2 when 0 then grade+10 else grade end as grade from stu
#3
SELECT id ,
grade + CASE WHEN grade % 2 = 0 THEN 10
ELSE 0
END AS grade
FROM stu
#4
select id,grade=case when grade%2=0 then grade+10 else grade end
from tb
#5
这是什么原因呢?看图
#6
如果查询的参数除了ID,GRADE,还有很多个,能不能slect *, grade + CASE WHEN grade % 2 = 0 THEN 10 ELSE 0 END AS grade
FROM stu
#7
但是我上面的写法,好像查询了两次grad,有没有办法只查询一次
#8
select * 默认查询的是显示全部,请问如何用sql语句在select * 中隐藏显示字段为id这一列
#9
木有办法
只能手动把所有其他列写上去。
#10
+1
#11
如果列实在很多,楼主可以考虑拼sql的方式。
if OBJECT_ID('A') is not null drop table A
create table A(id int,col1 nvarchar(1),col2 nvarchar(1),col3 nvarchar(1),col4 nvarchar(1),col5 nvarchar(1))
declare @columnList varchar(2000) --字段列集合字符串
select @columnList=isnull(@columnList+',','')+name from syscolumns where id=OBJECT_ID('A') and name<>'id'
--输出列名
print @columnList --拿着去拼查询语句
drop table A
#12
噢噢,谢谢
#13
不是说ID为偶数的加10分么,为嘛都是Grade%2????
#14
select id,case when id%2=0 then grade+10 else grade end as grade from yourtable
#15
进来拜一下楼主的结贴率
#16
select id,grade+case when grade%2=0
then 10
else 0
end as grade
from table_name;
then 10
else 0
end as grade
from table_name;