我有以下表格的查询:
UPDATE TableName SET some="1", fields="two"
WHERE some_condition="true"
IF @@ROWCOUNT=0 INSERT INTO TableName(some, fields) VALUES ("1", "two");
此查询是用MS SQL编写的,但我想将其翻译为MySQL。我找了几个说要用ROW_COUNT()替换@@ ROWCOUNT的裁判,但我不知道该怎么办。
如何将这样的查询从MS SQL转换为MySQL?
答案 0 :(得分:1)
处理这种情况的更优雅的方式就像....
IF EXISTS(SELECT 1 FROM TableName WITH (UPDLOCK, HOLDLOCK)
WHERE some_condition= "true" )
BEGIN
UPDATE TableName
SET some = "1"
, fields = "two"
WHERE some_condition= "true" ;
END
ELSE
BEGIN
INSERT INTO TableName(some, fields) VALUES ("1", "two");
END