I'm trying to perform the following query using 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
but it gives the following error:
Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+p.postid+'%' AND e.params LIKE '%parentid='+q.postid+'%' ORDER BY datetime DESC' at line 1
It seems it doesn't like the +
sign, which I thought was the correct way to add values in a query; guess not :) How do I ask it to check if the column value of one table (an integer) is in the column value of another table (a string)?
Edit: Thanks for the answers, here's the code that worked, using 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
try this one,
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
See the MySQL docs for CONCAT()
: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_concat
Such like:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.