MySQL 更新和插入语句
目前,有些数据输入不正确,我基本上需要运行一个模式来更新当前记录并插入新记录(如果可能的话,在一个语句中)。该表的设置如下:
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
SETtable
.locationType
= 5 WHEREtable
.locationType
= 0 ANDtable
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先按照您已经描述的那样执行更新:
然后复制所有记录并为它们分配 6 作为插入时的位置类型,记住通过 locationType = 5 限制记录,以确保新添加的记录也不会被复制(不确定)如果这甚至是 mysql 的一个问题,但只是为了确定):
它不是在一个语句中,但它会完成工作,如果您担心不一致,那么只需包装一个事务两个操作。
First execute the update as you described already:
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):
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.
您不能在 UPDATE 语句中执行此操作,更新仅更新表中已有的内容。
如果您想复制更改一个字段的条目,您可以执行以下操作:(
请注意,所有这些都作为一条语句执行)
另请注意,我们从表中选择 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:
(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.
我认为不可能在单个查询中完成此操作。但是,您可以插入重复行并将 locationType 设置为 6,然后将 locationTypes 更新为 0 到 5。以下是查询:
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: