我正在尝试创建一个查询字段的插入值的查询。我正在尝试使用子查询完成此操作,但卡住了。我想要完成的任务:
(message1) User1 says: Hello User2
(message2) ChatX says: User1, user2 said hello to you!
我认为为了实现这一点,我需要在like语句中使用子查询。
SELECT chat.id, chat.userid, chat.message, user.userid, user.username
FROM chat, user
WHERE LOWER(message) LIKE CONCAT('hello ', (SELECT user.username FROM user WHERE XXX = user.username))
AND chat.userid = user.userid
LIKE语句中的XXX是有人打招呼的用户名。它应该在那里检查用户表以及消息是否与用户输出ChatX的行匹配。我的问题是,如何让XXX工作以及如何设置?
答案 0 :(得分:1)
SELECT c.id, c.message,
sender.userid, sender.username,
receiver.userid, receiver.username
FROM chat c
JOIN user sender ON c.userid = sender.userid
JOIN user receiver ON LOWER(message) like CONCAT('hello ', receiver.username)