0

我正在使用 sql gui 工具“SQLite Database Browser 2.0 b1”中的“执行 SQL”功能。

我不确定我面临的问题是由于浏览器中的错误还是我的 SQL 错误。我知道我在浏览器工具中发现了几个错误。

无论如何。这是我目前正在处理的问题......

我在数据库中有两个表:

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 ......

我正在尝试更新 MATERIALS 表,以便如果与 TOYS 表中匹配的 TOY_ID 对应的 TOY_DESC 包含单词“plastic”,则它应该将数字 1 插入 MATERIALS 表的 PLASTIC 列中。这是我尝试过的:

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

当我尝试这个时,它什么也没做。

然后我试了...

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

然后给了我:

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

然后我试了...

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

并且只更新了一条记录:

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

但是,它仍然没有更新描述中包含塑料一词的所有记录!我不明白。为什么不是所有的记录都更新了?我可以理解单词末尾是否有其他类型的符号或字符,但没有。这就像其他描述中的完全相同的词,它们都被跳过了!我对为什么在我用双引号替换单引号之前更新根本不起作用感到困惑。这是因为数据库类型(sqlite 与 mysql)的不同吗?

编辑**:

我尝试了下面海报中的建议,进一步分解它,看看问题可能出在哪里。当我这样做时:

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

我成功获得了描述中包含塑料一词的所有记录的列表。所以 LIKE 语句不是问题。现在的问题是如何让这反映在 MATERIALS 表中?

我猜我以某种方式需要每场比赛的循环?

4

1 回答 1

1

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. I am no sqllite expert, but it looks as though %like% would require characters on both sides. 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'

UPDATE: Great, now you know you are getting the expected IDs and don't have to mess with that subquery anymore. Now try instead of WHERE TOY_ID = ( use WHERE TOY_ID in (

于 2013-10-08T23:14:45.543 回答