简体   繁体   English

如何使用 like 语句运行 SQL 更新查询

[英]How do I run an SQL update query using a like statement

I am trying to update a field in a table using an SQL update query where there is a like statement referencing a value in another table.我正在尝试使用 SQL 更新查询更新表中的字段,其中有一个引用另一个表中值的类似语句。 They syntax unfortunately is not working.不幸的是,它们的语法不起作用。 Below is my code.下面是我的代码。 In short, I am trying to put a '1' in the field 'Query07ParolaChiave' in the table 'tblSearchEngine01' when the value located in table 'tblsearchengine07' is present in the field 'tblMasterListOfEventsNotes' located in the table 'tblSearchEngine01'.简而言之,当位于表 'tblSearchEngine01' 中的字段 'tblMasterListOfEventsNotes' 中存在位于表 'tblSearchEngine07' 中的值时,我试图在表 'tblSearchEngine01' 中的字段 'Query07ParolaChiave' 中放置一个 '1'。 I think my code is almost complete but there is a syntax issue which i cant find.我认为我的代码几乎完成了,但是我找不到语法问题。

st_sql = "UPDATE tblSearchEngine01, tblSearchEngine07 SET tblSearchEngine01.Query07ParolaChiaveSelect = '1' WHERE ((([tblSearchEngine01].[tblMasterListOfEventsNotes]) Like " * " & [tblsearchengine07].[ParolaChiave] & " * "))"
    Application.DoCmd.RunSQL (st_sql)

I suggest you 2 solutions :我建议你2个解决方案:

This one is using EXISTS functions, and will check for each row in tblSearchEngine01 if there is a matching value in tblsearchengine07这个人使用EXISTS功能,并且将检查中的每一行tblSearchEngine01如果在匹配值tblsearchengine07

UPDATE 
    tblSearchEngine01
SET 
    tblSearchEngine01.Query07ParolaChiaveSelect = '1' 
WHERE 
    EXISTS (SELECT 1 
            FROM tblsearchengine07
            WHERE [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*')

This one is more performant because it uses JOIN这个性能更好,因为它使用JOIN

UPDATE 
    tblSearchEngine01 
    INNER JOIN tblsearchengine07 
        ON [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*'
SET 
    tblSearchEngine01.Query07ParolaChiaveSelect = '1'

I read something like in ADO/VBA, you have to use % instead of * as the wildcard.我在ADO/VBA 中读到过类似的内容,您必须使用%而不是*作为通配符。

You can have more information on wildcard and LIKE comparator here您可以在此处获得有关通配符和LIKE比较器的更多信息

UPDATE更新

Why the '1' after select in your first solution?为什么在您的第一个解决方案中选择后的“1”? EXISTS (SELECT 1 ... is better for performance because it return only the number 1 instead of fields, anyway EXISTS just stop the excecution after 1 element found. EXISTS (SELECT 1 ...对性能更好,因为它只返回数字 1 而不是字段,无论如何 EXISTS 只是在找到 1 个元素后停止执行。

'Performant' means more consuming in regards to space and memory? “高性能”意味着在空间和内存方面消耗更多? JOIN is more performant in term of time of execution, RDBMS are far better at joining tables than using subquery, in some rare case, it's more interesting to use the 1st solution. JOIN 在执行时间方面的性能更高,RDBMS 在连接表方面比使用子查询要好得多,在极少数情况下,使用第一种解决方案更有趣。

Also, any initial thoughts as to why my original solution (coming straight from an Access Query which works) does not function?另外,关于为什么我的原始解决方案(直接来自有效的 Access Query)不起作用的任何初步想法? I cannot really know but perhaps it's because of " * " , because you are saying SPACE + * + SPACE + VALUE + SPACE + * + SPACE .我真的不知道,但也许是因为" * " ,因为你说的是SPACE + * + SPACE + VALUE + SPACE + * + SPACE For ex : 'John' LIKE ' John '例如: 'John' LIKE ' John '

May be with "*" instead of " * " could solve it...可能用"*"代替" * "可以解决它......

I have no other track, I'm not Access sql developper, I usually play around Sql server/Oracle/mySql, hope it helped.我没有其他轨道,我不是 Access sql 开发人员,我通常玩 Sql server/Oracle/mySql,希望它有所帮助。 ;) ;)

尝试以这种方式改变你的喜好:

... Like '*" & tblsearchengine07.parolachiave & "*'))"

The like statement go into the WHERE clause. like 语句进入 WHERE 子句。

If you do want to use LIKE without you care about caps letters, then you can use it like this:如果您确实想在不关心大写字母的情况下使用 LIKE,那么您可以像这样使用它:

LIKE COLUMN_NAME = '%WhatYouLike%' LIKE COLUMN_NAME = '%WhatYouLike%'

My suggestion is:我的建议是:

  1. Use a table variable (@Table) with a unique/primary key coming from the table to be updated.使用表变量 (@Table) 和来自要更新的表的唯一键/主键。
  2. SELECT all the data to be updated (you can add the like statement here) and then INSERT that in the created table variable. SELECT 所有要更新的数据(您可以在此处添加 like 语句),然后将其插入到创建的表变量中。
  3. Construct the UPDATE statement with an INNER JOIN to the table variable matching with the unique/primary key.使用 INNER JOIN 构造与唯一/主键匹配的表变量的 UPDATE 语句。

I know this may take a lot of steps but believe me these are more efficient than using a black list approach.我知道这可能需要很多步骤,但相信我这些步骤比使用黑名单方法更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM