MySQL:循环遍历一个表并更新另一个表?
表 t1:
s (string) | x (int)
----------------+--------
"gfrdgeradfg" | 0
"abdfodpnmn" | 0
... | ...
表 t2:
c (varchar(1))
-----
"a"
"c"
"g"
"r"
-----
我想为 t1.s 中出现的每个字符 t2.c 添加 +1 到 t1.x,即结果应该是这样的:
s | x
----------------+--------
"gfrdgeradfg" | 3 (contains "a","g","r")
"abdfodpnmn" | 1 (contains "a")
... | ...
Looping through t2 and update t1 in php is相当简单,但如果可能的话,我宁愿用纯 SQL 来完成。
感谢您的帮助。
Table t1:
s (string) | x (int)
----------------+--------
"gfrdgeradfg" | 0
"abdfodpnmn" | 0
... | ...
Table t2:
c (varchar(1))
-----
"a"
"c"
"g"
"r"
-----
I would like to add +1 to t1.x for every character t2.c that occurs in t1.s, i.e. the result should be something like this:
s | x
----------------+--------
"gfrdgeradfg" | 3 (contains "a","g","r")
"abdfodpnmn" | 1 (contains "a")
... | ...
Looping through t2 and update t1 in php is quite straightforward, but I'd rather do it in pure SQL, if possible.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说明:表达式
t1.s LIKE CONCAT('%', t2.c, '%')
将计算为布尔值,相当于1
或MySQL 中的 0
。我没有测试它,所以如果它不起作用,请告诉我。
Clarification: The expression
t1.s LIKE CONCAT('%', t2.c, '%')
will evaluate to a Boolean which is equivalent to1
or0
in MySQL.I didn't test it so please tell me if it doesn't work.