1

我正在尝试使用 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
4

2 回答 2

4

试试这个,

SELECT e.event, 
       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.id = p.id        -- not sure on this. please supply
                                   -- the correct linking columnID
         LEFT JOIN qa_posts as q 
             ON e.id = q.id        -- and this
WHERE  (DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= datetime) AND 
       e.params LIKE CONCAT('%', 'postid=', p.postid, '%' )  AND 
       e.params LIKE CONCAT('%', 'parentid=', q.postid, '%' )  AND
       e.userid = 1
ORDER BY datetime DESC
于 2012-09-04T03:26:30.503 回答
1

请参阅 MySQL 文档CONCAT()http ://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_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 
         LEFT JOIN qa_posts as q ON e.userid=1 AND 
              DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= datetime AND 
              e.params LIKE CONCAT('%postid=',p.postid,'%') AND 
              e.params LIKE CONCAT('%parentid=',q.postid,'%')
ORDER BY datetime DESC
于 2012-09-04T03:27:16.060 回答