SQL更新查询

发布于 2024-09-17 01:50:33 字数 1084 浏览 4 评论 0原文

假设我想在数据库中执行更新,该数据库在同名表中保存例如汽车的信息。

我希望执行的更新包括将模型中表中的名称从“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 技术交流群。

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

发布评论

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

评论(3

各自安好 2024-09-24 01:54:23

如果您需要“Ford”而不是“Ford-”的不精确匹配,因为该字段中还有其他内容(Ford-让我认为可能存在),

请尝试以下操作:

UPDATE cars   
SET model= Replace(model, 'Ford','Ford-') 
WHERE model like 'Ford%' and model not like '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:

UPDATE cars   
SET model= Replace(model, 'Ford','Ford-') 
WHERE model like 'Ford%' and model not like 'Ford-%'   
谁的新欢旧爱 2024-09-24 01:53:34

它将寻找 exakt 匹配,

UPDATE cars SET model='Ford-' WHERE model='Ford' 

只会更改 model 恰好 == 'Ford' 的表

It will look for an exakt match,

UPDATE cars SET model='Ford-' WHERE model='Ford' 

will only alter tables where model is exactly == 'Ford'

对你的占有欲 2024-09-24 01:52:44

您是正确的:

... WHERE model='Ford'

是完全匹配(仅匹配“Ford”,不匹配“Ford-”)。对于子字符串匹配,您可以使用

... WHERE model LIKE 'Ford%'

(匹配以“Ford”开头的任何内容 - 但这显然不是您想要的)。

请参阅 UPDATE哪里

You are correct:

... WHERE model='Ford'

is an exact match (only matches "Ford", doesn't match "Ford-"). For a substring match, you'd use

... WHERE model LIKE 'Ford%'

(matches anything that starts with "Ford" - but that's apparently not what you want).

See the documentation for UPDATE and WHERE.

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