[英]MySQL query that checks in another table
I've got a problem with two mysql
tables. 我有两个mysql
表的问题。 I've done some code and I think I am close to the solution, but I'm not sure if this is right. 我已经完成了一些代码,我想我已经接近解决方案了,但是我不确定这是否正确。
So here are the two tables: 所以这是两个表:
Table 1: Blogs
Columns: ID, agp_name, agp_url, agp_username, agp_password
Table 2: Keywords
Columns: ID, agp_user_id, agp_order_id, agp_blog_id, agp_keywords, agp_keywords_date
What I want is to get one random row from Table1 based on the following condition: if the agp_keyword
match one of the keywords in the last 5 days then do not include into the result. 我想要的是根据以下条件从Table1中获得一个随机行:如果agp_keyword
在最近5天中与关键字之一匹配,则不包括在结果中。
So far I did this: 到目前为止,我做到了:
SELECT
t1.agp_user_id, t1.agp_order_id, t1.agp_blog_id, t1.agp_keywords, t1.agp_keywords_date, t2.agp_name, t2.agp_url, t2.agp_username, t2.agp_password
FROM table1 AS t1
INNER JOIN (
SELECT ID, agp_name, agp_url, agp_username, agp_password, agp_blogposts
FROM table2
) AS t2 ON t1.agp_blog_id = t2.ID
WHERE
t1.agp_keywords NOT LIKE "%keyword1%" AND
t1.agp_keywords NOT LIKE "%keyword2%" AND
t1.agp_keywords_date BETWEEN (1369440000 AND 1369932432)
ORDER BY RAND() LIMIT 1
However this does not work correctly. 但是,这不能正常工作。 Any help will be appreciated. 任何帮助将不胜感激。
Try this, your original specifications were a bit confusing :( 试试看,您的原始规格有点令人困惑:(
SELECT keywords.agp_user_id,
keywords.agp_order_id,
keywords.agp_blog_id,
keywords.agp_keywords,
keywords.agp_keywords_date,
blogs.agp_name,
blogs.agp_url,
blogs.agp_username,
blogs.agp_password
FROM blogs
LEFT JOIN keywords
ON keywords.agp_blog_id = blogs.ID
AND keywords.agp_keywords NOT LIKE "%keyword1%"
AND keywords.agp_keywords NOT LIKE "%keyword2%"
AND FROM_UNIXTIME(keywords.agp_keywords_date) > (DATE_SUB(CURDATE(), INTERVAL 5 DAYS))
ORDER BY RAND() LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.