Mysql SQL从表中选择最新结果,其中关键字=和竞争对手=

Mysql SQL从表中选择最新结果,其中关键字=和竞争对手=,mysql,sql,select,maxdate,Mysql,Sql,Select,Maxdate,我有两张这样的桌子: tablename: input_keywords Cols: keyword, last_check, pages_deep, check_freq_days tablename: rank_result Cols: keyword, result_url, position, last_check, competitor input_keywords.keyword是唯一的,因此我们不会查找多个关键字 rank_result保留爬网中的所有数据,并存储一些元数据 我

我有两张这样的桌子:

tablename: input_keywords
Cols: keyword, last_check, pages_deep, check_freq_days

tablename: rank_result
Cols: keyword, result_url, position, last_check, competitor
input_keywords.keyword是唯一的,因此我们不会查找多个关键字

rank_result保留爬网中的所有数据,并存储一些元数据

我需要展示以下内容

a = input_keywords
b = rank_result

a.keyword, a.last_check, b.position WHERE b.competitor = 'xxx'
但仅从排名结果中选择最后/最近的结果

我尝试了其他几个答案,但没有得到预期的结果


更新1 输入关键字的示例内容

----------------------------------------------------------------------------------------------------------------------------------
| id | keyword               | last_check          | CREATION            | MODIFICATION        | p_deep | check_freq_days | type |
----------------------------------------------------------------------------------------------------------------------------------
| 1  | acoustic guitars      | 2017-03-07 17:03:55 | 2017-01-20 12:27:17 | 2017-03-07 17:03:55 | 5      | 1               | NULL |
----------------------------------------------------------------------------------------------------------------------------------
| 2  | guitar accessories    | 2017-03-05 11:03:49 | 2017-01-20 12:27:27 | 2017-03-05 11:03:49 | 5      | 3               | NULL |
----------------------------------------------------------------------------------------------------------------------------------
| 3  | guitar amps           | 2017-03-05 11:04:05 | 2017-01-20 12:27:33 | 2017-03-05 11:04:06 | 5      | 3               | NULL |
----------------------------------------------------------------------------------------------------------------------------------
| 4  | guitar strings        | 2017-03-05 13:03:51 | 2017-01-20 12:27:42 | 2017-03-05 13:03:51 | 5      | 3               | NULL |
----------------------------------------------------------------------------------------------------------------------------------
| 5  | guitar effects pedals | 2017-03-05 11:03:43 | 2017-01-20 12:27:50 | 2017-03-05 11:03:43 | 5      | 3               | NULL |
----------------------------------------------------------------------------------------------------------------------------------



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| id  | keyword          | result_url                           | position | check_time          | useragent_used                       | proxy_log | check_date | competitor   |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 493 | acoustic guitars | http://www.competitor.com/… | 1        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | Competitor1      |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 494 | acoustic guitars | http://competitor2.com… | 2        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | Competitor2   |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 495 | acoustic guitars | https://out_website.com | 3        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | US    |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 496 | acoustic guitars | http://competitor3.com | 4        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | Competitor3 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 497 | acoustic guitars | http://competitor4.com | 5        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | NULL         |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 498 | acoustic guitars | https://www.amazon.co.uk/acoustic-g… | 6        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | Amazon       |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 499 | acoustic guitars | http://compx.com | 7        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | compX          |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 500 | acoustic guitars | http://compx.com | 8        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | compX      |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 501 | acoustic guitars | http://www. compx.com/  | 9        | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | NULL         |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 502 | acoustic guitars | http://www. compx.com… | 10       | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | NULL         |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 503 | acoustic guitars | http://www. compx.com/        | 11       | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | NULL         |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 504 | acoustic guitars | http://www. compx.com… | 12       | 2017-01-18 09:36:17 | Mozilla/5.0 (compatible; MSIE 10.0;… | NULL      | 2017-01-18 | NULL         |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
此表记录了我们在中检查关键字排名的每一天 (因此同一关键字和竞争对手有很多记录)

我只需要我们的排名,如果我们有一个,否则只返回关键字

我的理想输出是

|keyword  | last_check | p_deep | check_freq_days | position |
|keyword1 | 2017-03-06 | 5      |.  3.            | 4.       |
|keyword2 | 2017-03-06 | 5.     |.  3.            | NULL.    |
我最好的尝试是:

select input_keywords.keyword, input_keywords.last_check, input_keywords.p_deep, `input_keywords`.`check_freq_days`, rank_result.position from input_keywords join rank_result on input_keywords.keyword=rank_result.keyword where rank_result.competitor = 'OurCompany' and input_keywords.last_check=rank_result.check_time
这几乎起作用,但如果我们没有被列入名单,就不会返回结果


当我从
输入\u关键字表开始时,我希望列出所有关键字,以及我们当前的排名(如果有的话)。

添加
排名结果\u id
字段作为主要和自动增量
添加字段后,按
rank\u result\u id
DESC

获取数据顺序,我通过执行以下操作解决了这个问题

  • 将竞争对手字段添加到
    输入\u关键字
    表中
  • 添加了一些索引以加快查询速度
  • 创建了以下SQL

    SELECT i.`keyword`, i.`last_check`, i.`p_deep`, i.`check_freq_days`, r.`position`
      FROM input_keywords i
      LEFT JOIN rank_result r 
        ON i.`keyword` = r.`keyword` AND i.`competitor` = r.`competitor` AND i.`last_check` = r.`check_time`
    

    然后,每个输入关键字返回一行,其中包含我们在排名中的最后一个已知位置

    样本数据和期望结果,这将有助于解释您正在尝试做什么。请添加更多详细信息,您尝试了什么,期望得到什么,以及您得到了什么。您必须提供有关您的问题的更多详细信息,比如你失败的尝试,这很重要。如果提问者没有表明他的努力,我们就不能给出完整的答案。他的问题应该与我们的问题相同
    SELECT i.`keyword`, i.`last_check`, i.`p_deep`, i.`check_freq_days`, r.`position`
      FROM input_keywords i
      LEFT JOIN rank_result r 
        ON i.`keyword` = r.`keyword` AND i.`competitor` = r.`competitor` AND i.`last_check` = r.`check_time`