我正在尝试使用 MySQL 执行以下查询:
SELECT e.event,
BINARY e.params as params,
UNIX_TIMESTAMP(e.datetime) AS datetime,
p.postid AS postid,
q.postid AS parentid
FROM qa_eventlog as e
LEFT JOIN qa_posts as p
LEFT JOIN qa_posts as q ON e.userid=1 AND
DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= datetime AND
e.params LIKE '%postid='+p.postid+'%' AND
e.params LIKE '%parentid='+q.postid+'%'
ORDER BY datetime DESC
但它给出了以下错误:
错误 1064:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '+p.postid+'%' AND e.params LIKE '%parentid='+q.postid+'%' ORDER BY datetime DESC' 附近使用正确的语法
它似乎不喜欢这个+
标志,我认为这是在查询中添加值的正确方法;猜不:) 我如何要求它检查一个表(整数)的列值是否在另一个表(字符串)的列值中?
编辑:感谢您的回答,这是有效的代码,使用CONCAT
:
SELECT e.event,
BINARY e.params as params,
UNIX_TIMESTAMP(e.datetime) AS datetime,
p.postid AS postid,
q.postid AS parentid
FROM qa_eventlog AS e
LEFT JOIN qa_posts AS p
ON e.params LIKE CONCAT('%postid=', p.postid, '%' )
LEFT JOIN qa_posts AS q
ON e.params LIKE CONCAT('%postid=', q.postid, '%' )
WHERE e.userid=1
AND DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= datetime
ORDER BY datetime DESC