更新查询不会影响数据库中的所有行
我有以下查询......
UPDATE vehicle_catalog SET parent_id = 0 WHERE parent_id = SUBSTR(id, 0, 5)
我需要将所有 parent_id
设置为 0,其中 id 的前 5 个字符与parent_id 相同。当我查看数据时,这会影响 0 行,而它应该会影响超过 10,000 行。
关于为什么这不会影响所有行的任何想法?
更新 如果我这样做了,我不明白什么 UPDATE SET col = '1'
它会影响整个数据库,但我的查询并未在整个数据库上运行。
更新 正确的解决方案:
UPDATE `vehicle_catalog` SET parent = 0 WHERE SUBSTRING(id FROM 1 FOR 6) == SUBSTRING(parent_id FROM 1 FOR 6)
I have the following query....
UPDATE vehicle_catalog SET parent_id = 0 WHERE parent_id = SUBSTR(id, 0, 5)
I need to set all parent_id
s to 0 where the first 5 characters of id is the same as the parent_id. This is effecting 0 rows when I'm looking at the data and it should be effecting over 10,000.
Any ideas on why this wouldn't be effecting all rows?
UPDATE
What I don't understand if I didUPDATE SET col = '1'
it would effect the whole database, yet my query isn't run on the whole database.
UPDATE
The proper solution:
UPDATE `vehicle_catalog` SET parent = 0 WHERE SUBSTRING(id FROM 1 FOR 6) == SUBSTRING(parent_id FROM 1 FOR 6)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MySQL 中的 SUBSTR() 使用 1-基于索引。试试这个:
另外,请参阅我关于使用 SELECT 查询来验证您的条件是否正确构造的评论。
SUBSTR() in MySQL uses 1-based indexing. Try this:
Also, see my comment on using a SELECT query to verify that your condition is correctly constructed.
只是一个猜测,但是
WHERE Parent_id = SUBSTR(id, 0, 5)
,或者,应该是...just a guess, but
WHERE parent_id = SUBSTR(id, 0, 5)
, or, should it be...您的更新只会影响所写的一行。例如,如果 ID 为 12345678,则您的查询只会更新第 12345 行。您可以尝试这样做
your update will only affect a single row as written. for instance, if the ID was 12345678, your query would only update row 12345. You might try this