MySQL 更新和插入语句

发布于 2024-10-28 13:00:00 字数 597 浏览 4 评论 0原文

目前,有些数据输入不正确,我基本上需要运行一个模式来更新当前记录并插入新记录(如果可能的话,在一个语句中)。该表的设置如下:

  cityID            int(10)
  stateID           smallint(5)
  orderEnterpriseID int(10)
  isDefault         tinyint(3)
  locationType      tinying(2)

现在,每条记录的 locationType 为 0,我需要将其更新为 5:

更新
SET
位置类型 = 5 WHERE .locationType = 0 AND .orderFromEnterprise = 0;

但我还需要创建另一条包含重复数据的记录,并将 locationType 设置为 6。我在理解其中一些 SQL 语句时遇到问题,非常感谢任何帮助!!!!

Currently there's some data that was entered incorrectly and I basically need to run a schema which updates the current record and inserts a new one (in one statement if possible). The table is set up as so:

  cityID            int(10)
  stateID           smallint(5)
  orderEnterpriseID int(10)
  isDefault         tinyint(3)
  locationType      tinying(2)

Right now, every record has a locationType of 0 and I need to update it to 5:

UPDATE
table SET
table.locationType
= 5 WHERE table.locationType
= 0 AND table.orderFromEnterprise
= 0;

But I also need to create another record with duplicate data and set the locationType to 6. I have issues wrapping my head around some of these SQL statements and any help is greatly appreciated!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

围归者 2024-11-04 13:00:00

首先按照您已经描述的那样执行更新:

UPDATE
table SET
table.locationType = 5 WHERE table.locationType = 0 AND table.orderFromEnterprise = 0;

然后复制所有记录并为它们分配 6 作为插入时的位置类型,记住通过 locationType = 5 限制记录,以确保新添加的记录也不会被复制(不确定)如果这甚至是 mysql 的一个问题,但只是为了确定):

INSERT INTO table 
(cityID, stateID, orderEnterpriseID, isDefault, locationType) 
SELECT t1.cityID, t1.stateID, t1.orderEnterpriseID, t1.isDefault, 6 
FROM table as t1
WHERE t1.locationType = 5

它不是在一个语句中,但它会完成工作,如果您担心不一致,那么只需包装一个事务两个操作。

First execute the update as you described already:

UPDATE
table SET
table.locationType = 5 WHERE table.locationType = 0 AND table.orderFromEnterprise = 0;

Then copy all records and assign them a 6 as location type on the insertion, remember to limit the records by locationType = 5, to make sure that newly added records are not copied as well (not sure if thats even an issue with mysql but just to be sure):

INSERT INTO table 
(cityID, stateID, orderEnterpriseID, isDefault, locationType) 
SELECT t1.cityID, t1.stateID, t1.orderEnterpriseID, t1.isDefault, 6 
FROM table as t1
WHERE t1.locationType = 5

Its not in one statement but it will get the job done, if you're worried about inconsistencies then just wrap a transaction around the two operations.

别在捏我脸啦 2024-11-04 13:00:00

您不能在 UPDATE 语句中执行此操作,更新仅更新表中已有的内容。

如果您想复制更改一个字段的条目,您可以执行以下操作:(

INSERT table_name (cityID, stateID , orderEnterpriseID, isDefault, locationType)
SELECT cityID, stateID , orderEnterpriseID, isDefault, 6
FROM table_name

请注意,所有这些都作为一条语句执行)
另请注意,我们从表中选择 6 而不是 locationType。

我们在这里所做的只是选择表中的所有内容,将位置类型替换为 6,然后将其插入回表中。

you can't do that within the UPDATE statement, update only updates what already is in the table.

if you want to duplicate your entries changing one field you could do the following:

INSERT table_name (cityID, stateID , orderEnterpriseID, isDefault, locationType)
SELECT cityID, stateID , orderEnterpriseID, isDefault, 6
FROM table_name

(please note, that all of this is executed as one statement)
also note that we select 6 instead of locationType from the table.

What we do here is simply select everything from the table substituting location type to 6 and inserting this back into the table.

爱*していゐ 2024-11-04 13:00:00

我认为不可能在单个查询中完成此操作。但是,您可以插入重复行并将 locationType 设置为 6,然后将 locationTypes 更新为 0 到 5。以下是查询:

insert into table (cityID, stateID, orderEnterpriseID, isDefault, locationType)
select cityID, stateID, orderEnterpriseID, isDefault, 6
from table
where locationType = 0 and orderEnterpriseID = 0;

# now your update query
update table
set locationType = 5
where locationType = 0 and orderEnterpriseID = 0;

I don't think it is possible to do this in a single query. You can, however, insert duplicate rows with locationType set to 6 and then update locationTypes of 0 to 5. Here are the queries:

insert into table (cityID, stateID, orderEnterpriseID, isDefault, locationType)
select cityID, stateID, orderEnterpriseID, isDefault, 6
from table
where locationType = 0 and orderEnterpriseID = 0;

# now your update query
update table
set locationType = 5
where locationType = 0 and orderEnterpriseID = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文