如果数据MySQL没有变化,则更新MySQL表的数据

时间:2013-02-05 12:27:35

标签: mysql sql sql-server

如果数据没有变化,我想更新表格,在 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 Server

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

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

1 个答案:

答案 0 :(得分:0)

我仍然不知道你为什么要这样做......但是如何使用WHERE子句

类似的东西:

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

希望这会有所帮助