简体   繁体   English

使用UPDATE,SET,WHERE和LIKE的Sqlite语句未更新所有记录

[英]Sqlite statement using UPDATE, SET, WHERE, and LIKE isn't updating all of the records

I am using the 'Execute SQL' feature in sql gui tool 'SQLite Database Browser 2.0 b1.' 我在sql gui工具“ SQLite数据库浏览器2.0 b1”中使用“执行SQL”功能。

I am not sure if the problem I am facing is because of a bug within the Browser or if my SQL is just wrong. 我不确定我面临的问题是由于浏览器中的错误还是我的SQL错误。 I do know that I have found several bugs within the Browser tool. 我确实知道我在浏览器工具中发现了几个错误。

Anyhow. 无论如何。 Here is the problem I am dealing with currently... 这是我目前正在处理的问题...

I have two tables in a database: 我在数据库中有两个表:

TOYS
------------------------------
TOY_ID   TOY_DESC
------------------------------
1         Green truck, plastic
2         Doll, plastic, wood
3         Stuffed cloth animal
4         Board game cardboard and plastic
etc .....

MATERIALS
-----------------------------
TOY_ID     PLASTIC
-----------------------------
1
2
3
4
etc ......

I am trying to update the MATERIALS table so that IF the TOY_DESC that corresponds with the matching TOY_ID in the TOYS table, contains the word 'plastic', that it should insert the number 1 into the PLASTIC column in the MATERIALS table. 我正在尝试更新MATERIALS表,以便如果与TOYS表中匹配的TOY_ID对应的TOY_DESC包含单词“ plastic”,则应将数字1插入MATERIALS表的PLASTIC列中。 This is what I have tried: 这是我尝试过的:

UPDATE MATERIALS SET PLASTIC = 1 WHERE TOY_ID = (SELECT TOY_ID FROM TOYS WHERE TOY_DESC LIKE '%plastic%');

when I tried this it did nothing. 当我尝试这样做时,它什么也没做。

then I tried... 然后我尝试了...

UPDATE MATERIALS SET PLASTIC = 1 WHERE TOY_ID = (SELECT TOY_ID FROM TOYS WHERE TOY_DESC LIKE "%plastic%");

which then gave me: 然后给了我:

MATERIALS
-----------------------------
TOY_ID     PLASTIC
-----------------------------
 1          1
 2
 3
 4
 etc ......

then I tried... 然后我尝试了...

UPDATE MATERIALS SET PLASTIC = 1 WHERE TOY_ID = (SELECT TOY_ID FROM TOYS WHERE TOY_DESC LIKE '%plastic,%');

and that updated only one more record: 并且仅更新了一条记录:

MATERIALS
-----------------------------
TOY_ID     PLASTIC
-----------------------------
1            1
2            1
3
4
etc ......

But still, it didn't update ALL of the records that contained the word plastic in the description!! 但是,它并没有更新描述中包含单词plastic的所有记录! I don't get it. 我不明白 why aren't all the records updating? 为什么不是所有记录都在更新? I could understand if there was some other type of symbol or character on the end of the words, but there isn't. 我可以理解单词的末尾是否还有其他类型的符号或字符,但没有。 it's like exactly the same word in the other descriptions and they are ALL being skipped! 就像其他说明中的完全相同的单词一样,它们都被跳过了! I am further confused as to why the update didn't work at ALL until I replaced the single quotations with double quotations. 对于为什么直到我将单引号替换为双引号之前,更新无法全部生效的问题,我感到更加困惑。 Is this because of a difference in database type (sqlite vs. mysql)? 这是因为数据库类型不同(sqlite与mysql)吗?

EDIT**: 编辑**:

I tried the suggestion from the poster below to break it down further to see where the problem might be. 我尝试了以下海报中的建议,将其进一步分解以查看问题所在。 When I just do: 当我刚做的时候:

SELECT TOY_ID FROM TOYS WHERE TOY_DESC LIKE '%plastic%'

I successfully get a list of all the records that contain the word plastic in the description. 我成功获取了描述中包含单词单词plastic的所有记录的列表。 So the LIKE statement isn't the problem. 因此,LIKE语句不是问题。 The question now is how do I get this to be reflected in the MATERIALS table? 现在的问题是如何使它反映在“材料”表中?

I'm guessing I somehow need a loop for each match? 我猜我每次比赛都需要循环吗?

Troubleshoot your query by breaking down the components of it. 通过分解查询的组件对查询进行故障排除。 Run nested/sub queries first: 首先运行嵌套/子查询:

SELECT TOY_ID FROM TOYS WHERE TOY_DESC LIKE "%plastic%"

Does it give you the results you expect? 它给您您期望的结果吗?

Now you can focus on the like expression. 现在,您可以专注于like表达式。 I am no sqllite expert, but it looks as though %like% would require characters on both sides. 我不是sqllite专家,但是%like%似乎需要两面都用字符。 So you could do: 因此,您可以执行以下操作:

SELECT TOY_ID FROM TOYS 
WHERE 
     TOY_DESC LIKE "%plastic%" 
  OR TOY_DESC LIKE "plastic%" 
  OR TOY_DESC LIKE "%plastic"

There are some other possibilities here: SQLite query, 'LIKE' 这里还有其他可能性: SQLite查询,“ LIKE”

UPDATE: Great, now you know you are getting the expected IDs and don't have to mess with that subquery anymore. 更新:太好了,现在您知道您已经获得了预期的ID,而不必再与该子查询混淆了。 Now try instead of WHERE TOY_ID = ( use WHERE TOY_ID in ( 现在尝试代替WHERE TOY_ID = ( WHERE TOY_ID in (

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

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