0

我已经使用以下查询来更新我在 mysql 中的用户表

update users set user_likes = 
(CASE WHEN like_by_users NOT LIKE '%user_name%' 
AND dislikes_by_users NOT LIKE '%user_name%' 
THEN (user_likes +1) ELSE user_likes END)
WHERE post_id = '2221'

这对我来说很好,但我也想像这样更新 likes_by_users

likes_by_users = CONCAT(likes_by_users,' ','user_name')

我试过这样做

update users set user_likes = 
(CASE WHEN like_by_users NOT LIKE '%user_name%' 
AND dislikes_by_users NOT LIKE '%user_name%' 
THEN (user_likes +1), likes_by_users = CONCAT(likes_by_users,' ','user_name')  
ELSE user_likes END)
WHERE post_id = '2221'

但它根本不起作用。我不想再次检查条件,但如果上述条件为真,那么它不仅应该更新 (user_likes) 和 (likes_by_users) 两列 (user_likes),如果条件为假,那么它不应该更新任何列。

4

2 回答 2

0

像这样试试

update users
Set user_likes = IF(like_by_users NOT LIKE '%user_name%' AND dislikes_by_users NOT LIKE '%user_name%',(user_likes + 1), user_likes),
  likes_by_users = IF(like_by_users NOT LIKE '%user_name%' AND dislikes_by_users NOT LIKE '%user_name%', CONCAT(likes_by_users,' ','user_name'), user_likes)
WHERE post_id = 2221
于 2013-02-28T11:09:22.640 回答
0

您可以在 WHERE 子句的帮助下做到这一点。

UPDATE `users`
SET `user_likes` = `user_level` + 1, `likes_by_users` = CONCAT(`likes_by_users`, ' ', 'user_name')
WHERE `post_id` = '2221'
AND `like_by_users` NOT LIKE '%user_name%' 
AND `dislikes_by_users` NOT LIKE '%user_name%'
于 2013-02-28T11:02:23.347 回答