다음과 같은 두 개의 테이블이 있습니다.
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'
그러나 rank_result에서 LAST / 가장 최근 결과 만 선택합니다.
나는 몇 가지 다른 답변을 따르려고 시도했지만 내가 기대하는 결과를 얻지 못했습니다.
CREATE TABLE `input_keywords` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`keyword` char(150) DEFAULT NULL COMMENT 'the keyword....',
`last_check` timestamp NULL DEFAULT '2000-01-01 00:00:00' COMMENT 'Last check timestamp, default to years ago so we check immediatly',
`CREATION` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`MODIFICATION` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`p_deep` int(1) DEFAULT '5' COMMENT 'how many pages deep to search - default 5',
`check_freq_days` int(11) DEFAULT '3' COMMENT 'how often to check this keyword in DAYS default 3',
`type` char(50) DEFAULT NULL COMMENT 'Product, Category, other etc',
PRIMARY KEY (`id`),
UNIQUE KEY `UNQ_Keyword` (`keyword`)
) ENGINE=InnoDB AUTO_INCREMENT=5865 DEFAULT CHARSET=utf8;
CREATE TABLE `rank_result` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`keyword` char(150) DEFAULT '',
`result_url` text,
`position` int(11) DEFAULT NULL,
`check_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`useragent_used` char(255) DEFAULT NULL,
`proxy_log` text,
`check_date` date DEFAULT NULL COMMENT 'date of the check - easier for graph plotting',
`competitor` tinytext,
PRIMARY KEY (`id`),
KEY `idx_KEYWORD` (`keyword`),
KEY `keyword` (`keyword`,`check_time`,`competitor`(50))
) ENGINE=InnoDB AUTO_INCREMENT=2868936 DEFAULT CHARSET=utf8;
input_keywords의 예제 내용
----------------------------------------------------------------------------------------------------------------------------------
| 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
그리고 이것은 거의 작동하지만 목록에 없으면 결과를 반환하지 않습니다.
input_keywords
표 에서 시작 하면서 모든 키워드와 현재 순위 (있는 경우)를 나열하려고했습니다.
나는 다음을 수행하여 이것을 해결했습니다.
input_keywords
테이블에 경쟁사 필드 추가다음 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`
그러면 순위에서 마지막으로 알려진 위치와 함께 입력 키워드 당 단일 행이 반환됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다