如何在 SQL 中使用 Substring 进行更新?
我再次遇到 SQL 问题。我想将一个子字符串(原本用于 SELECT 查询)用于表 UPDATE。查询如下所示:
SELECT SUBSTRING(h.action,LOCATE('"',h.action)+1,(((LENGTH(h.action))-LOCATE('"', REVERSE(h.action))-1)-LOCATE('"',h.action))) AS File,
h.TIMESTAMP,
h.user,
d.uid,
d.size
d.id
from history h
INNER JOIN data d ON h.contract = d.contract
LEFT JOIN history ON d.user = history.user
WHERE ( SELECT DISTINCT SUBSTRING(h.action,LOCATE('"',h.action)+1,(((LENGTH(h.action))-LOCATE('"', REVERSE(h.action))-1)-LOCATE('"',h.action)))) IN (SELECT filename FROM data)
and h.contract=xy AND h.action LIKE 'file%added' GROUP BY File
现在我想更新表数据。列“user”和列“Timestamp”将从表历史记录传输到数据。我对更新命令的最后一次尝试如下:
update data d
set
d.user = v.user,
d.upload= h.`timestamp`
inner join
history h on d.contract = h.contract
where
d.filename in (SELECT DISTINCT SUBSTRING(h.action,LOCATE('"',h.action)+1,(((LENGTH(h.action))-LOCATE('"', REVERSE(h.action))-1)-LOCATE('"',h.action)))) and h.action like 'File%added'
到目前为止,我收到一个语法错误,或者它是一个返回 0 行进行修改的 SQL 命令。我希望这是一个容易解决的问题。
预先感谢您的帮助!
once again I have a problem in SQL. I want to use a substring, which I otherwise used for a SELECT query, for a table UPDATE. The query looks like this:
SELECT SUBSTRING(h.action,LOCATE('"',h.action)+1,(((LENGTH(h.action))-LOCATE('"', REVERSE(h.action))-1)-LOCATE('"',h.action))) AS File,
h.TIMESTAMP,
h.user,
d.uid,
d.size
d.id
from history h
INNER JOIN data d ON h.contract = d.contract
LEFT JOIN history ON d.user = history.user
WHERE ( SELECT DISTINCT SUBSTRING(h.action,LOCATE('"',h.action)+1,(((LENGTH(h.action))-LOCATE('"', REVERSE(h.action))-1)-LOCATE('"',h.action)))) IN (SELECT filename FROM data)
and h.contract=xy AND h.action LIKE 'file%added' GROUP BY File
Now I want to update the table data. The column 'user' and the column 'Timestamp' are to be transferred from the table history to data. My last attempt for the update command was the following:
update data d
set
d.user = v.user,
d.upload= h.`timestamp`
inner join
history h on d.contract = h.contract
where
d.filename in (SELECT DISTINCT SUBSTRING(h.action,LOCATE('"',h.action)+1,(((LENGTH(h.action))-LOCATE('"', REVERSE(h.action))-1)-LOCATE('"',h.action)))) and h.action like 'File%added'
Either I received a syntax error so far or it was a SQL command which returned 0 rows to modify. I hope that is an easy problem to fix.
Thank you in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道下面的查询是否有帮助,因为正如我在评论中提到的,您应该提供数据示例和预期结果。
正确的 UPDATE 语法是:
所以你的更新查询应该是:
I don't know if below query would help, because as I mentioned in the comment you should provide data examples and expected results.
The correct UPDATE syntax is:
So your update query should be: