SQL更新查询
假设我想在数据库中执行更新,该数据库在同名表中保存例如汽车的信息。
我希望执行的更新包括将模型中表中的名称从“Ford”更改为“Ford-”,即仅更改名称为“Ford”的位置,而不更改那些已经具有模型名称“Ford-”的模型“
通过运行下面的 SQL 查询我会得到我期望的结果吗?
UPDATE cars
SET model='Ford-' WHERE model='Ford'
在我看来,这应该只更新型号名称为“Ford”的汽车实例,并将其更改为“Ford-”,并且不会继续尝试更新名称已经为“Ford-”的任何汽车,或者什么?
如果确实如此,我在想是否有一个纯 SQL 查询结构可用于确保仅更改型号为“Ford”的汽车,而不更改型号为“Ford-”的汽车,
因为我不确定SQL 将在上面的查询中识别搜索字符串“Ford”,即 SQL 将查找完全匹配,或者模型中单词“Ford-”的存在会触发“误报”吗?好几年没做过SQL查询了。
不要谈论正常形式等,我不负责创建数据库/表等。我只是在研究可能清理数据库中的一些信息。
桌车长这样:
cars 'id', 'int(10) unsigned', '', 'PRI', '', 'auto_increment' 'something0', 'varchar(50)', '', 'UNI', '', '' 'something1', 'varchar(50)', 'YES', '', '', '' 'something2', 'varchar(50)', 'YES', '', '', '' 'something3', 'text', 'YES', '', '', '' 'something4', 'text', 'YES', '', '', '' 'something5', 'varchar(50)', 'YES', '', '', '' 'something6', 'varchar(50)', 'YES', '', '', '' 'something7', 'varchar(15)', 'YES', '', '', '' 'model', 'varchar(255)', 'YES', '', '', ''
欢迎评论
Say I wanted to perform an update in a database that held information on for example cars in a table by that same name.
The update I am looking to perform consists in changing the designation in the table in model from "Ford" to "Ford-" i.e. only changing where the designatin is "Ford" and not in those that already have a model designation of "Ford-"
By running the below SQL query will I get the result I expect ?
UPDATE cars
SET model='Ford-' WHERE model='Ford'
This in my opinion should update only instances of cars where the model designation is "Ford" and change those to "Ford-" and would not proceed to try to update anything where the designation is "Ford-" already as well, or what ?
If it does I was thinking if there is a pure SQL query structure that could be used to ensure that only cars with model = "Ford" was changed and not cars with model = "Ford-"
I am asking as I am uncertain on the way SQL would identify the search string "Ford" in the query above i.e. will SQL look for an exact match, or will the existence of the word "Ford-" in model trigger a "false positive" ? Have not done SQL queries for years.
Don't talk about normal forms or the like, I am not responsible for creating the DB/tables etc. I am just looking into possibly cleaning up some of the information in the DB.
The table cars looks like this:
cars 'id', 'int(10) unsigned', '', 'PRI', '', 'auto_increment' 'something0', 'varchar(50)', '', 'UNI', '', '' 'something1', 'varchar(50)', 'YES', '', '', '' 'something2', 'varchar(50)', 'YES', '', '', '' 'something3', 'text', 'YES', '', '', '' 'something4', 'text', 'YES', '', '', '' 'something5', 'varchar(50)', 'YES', '', '', '' 'something6', 'varchar(50)', 'YES', '', '', '' 'something7', 'varchar(15)', 'YES', '', '', '' 'model', 'varchar(255)', 'YES', '', '', ''
Comments are very welcome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要“Ford”而不是“Ford-”的不精确匹配,因为该字段中还有其他内容(Ford-让我认为可能存在),
请尝试以下操作:
if you need an inexact match on 'Ford' but not 'Ford-' because there is something else in the field as well (which Ford- makes me think there might be)
try this:
它将寻找 exakt 匹配,
只会更改 model 恰好 == 'Ford' 的表
It will look for an exakt match,
will only alter tables where model is exactly == 'Ford'
您是正确的:
是完全匹配(仅匹配“Ford”,不匹配“Ford-”)。对于子字符串匹配,您可以使用
(匹配以“Ford”开头的任何内容 - 但这显然不是您想要的)。
请参阅 UPDATE 和 哪里。
You are correct:
is an exact match (only matches "Ford", doesn't match "Ford-"). For a substring match, you'd use
(matches anything that starts with "Ford" - but that's apparently not what you want).
See the documentation for UPDATE and WHERE.