求一条SQL语句,本人新手
根据ID查出某个字段的值,如果该值为1就修改为0,值为0就修改为1.
能不能用一条SQL语句实现?
[解决办法]
update tb set col=1-col where id=@id
[解决办法]
--如果数据中除了1和0以外有其他数字的话会有影响DECLARE @T TABLE(ID INT,[COL] INT)INSERT @TSELECT 1,1 UNION ALLSELECT 2,1 UNION ALLSELECT 3,0 UNION ALLSELECT 4,0 UNION ALLSELECT 5,2 UPDATE @T SET COL=COL^1 FROM @T SELECT * FROM @T/*ID COL----------- -----------1 02 03 14 15 3*/--如果数据中除了1和0以外有其他数字的话会有影响DECLARE @T1 TABLE(ID INT,[COL] INT)INSERT @T1SELECT 1,1 UNION ALLSELECT 2,1 UNION ALLSELECT 3,0 UNION ALLSELECT 4,0 UNION ALLSELECT 5,2 UPDATE @T1 SET COL=1-COL FROM @T1 --WHERE ID=1SELECT * FROM @T1/*ID COL----------- -----------1 02 03 14 15 -1*/--如果数据中除了1和0以外有其他数字的话没有影响DECLARE @T2 TABLE(ID INT,[COL] INT)INSERT @T2SELECT 1,1 UNION ALLSELECT 2,1 UNION ALLSELECT 3,0 UNION ALLSELECT 4,0 UNION ALLSELECT 5,2 UPDATE @T2 SET COL=CASE COL WHEN 1 THEN 0 WHEN 0 THEN 1 ELSE COL END FROM @T2 --WHERE ID=1SELECT * FROM @T2/*ID COL----------- -----------1 02 03 14 15 2*/