-4

如果数据没有变化,我想更新一个表,在SQL Server中是可能的,但在 MySQL 中我怎么能?

MySQL

DROP TABLE IF EXISTS PEOPLE;
CREAte tempORARY  table PEOPLE
(
    _data CHAR(1),
    id int
);

INSERT into PEOPLE
SELECT 'N',1 UNION ALL 
SELECT 'Y',2 UNION ALL 
SELECT 'N',3 UNION ALL 
SELECT 'Y',4;

-- now I want to update it
UPDATE PEOPLE
SET _data = 'A'
WHERE id = 1;
-- 1 row(s) affected

-- now I want to update it again
UPDATE PEOPLE
SET _data = 'A'
WHERE id = 1;
-- 0 row(s) affected

如果数据没有变化,我想更新数据。

SQL 服务器

DECLARE @tempORARY  table
(
    _data CHAR(1),
    id int
);

INSERT into @tempORARY 
SELECT 'N',1 UNION ALL 
SELECT 'Y',2 UNION ALL 
SELECT 'N',3 UNION ALL 
SELECT 'Y',4;

-- now I want to update it
UPDATE @tempORARY 
SET _data = 'A'
WHERE id = 1;
-- 1 row(s) affected

-- now I want to update it again
UPDATE @tempORARY 
SET _data = 'A'
WHERE id = 1;
-- 1 row(s) affected

我希望我解决了我的问题,如果您仍有疑问,请发表评论。

4

1 回答 1

0

我仍然不知道你为什么要做这样的事情......但是使用 WHERE 子句怎么样

就像是:

UPDATE @tempORARY 
SET _data = 'A'
WHERE id = 1 AND _data != 'A';

希望这可以帮助

于 2013-02-05T12:38:27.170 回答