2014-02-17 2 views
0

나는 워드 프레스 사이트가 있고 외부 스크립트를 통해 외부 응용 프로그램으로 정보를 끌어 와야합니다.특정 mysql 복잡한 쿼리 조인

임은 하나의 MySQL의 호출에 약간의 문제가

내가 다음 wp_posts 표에 나열된 할인과 wp_postmeta 테이블의 시작 날짜와 종료 날짜가 있습니다.

그러나 나는 ONE MySQL은 쿼리 내 인생

사람이 그것을 어떻게 말해 줄 수 (시작과 끝 날짜 사이 즉) 라이브있는 모든 할인 혜택을 선택할 수 있습니다. 내가 4 예 2 덤프 반환해야이 아래

이 (104745,104744) 1 오래된 할인입니다 (104743) 1이 아직되지 않고 있습니다 (104666)

많은 감사

DROP TABLE IF EXISTS `wp_postmeta`; 
CREATE TABLE `wp_postmeta` (
    `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `post_id` bigint(20) unsigned NOT NULL DEFAULT '0', 
    `meta_key` varchar(255) DEFAULT NULL, 
    `meta_value` longtext, 
    PRIMARY KEY (`meta_id`), 
    KEY `post_id` (`post_id`), 
    KEY `meta_key` (`meta_key`) 
) ENGINE=InnoDB AUTO_INCREMENT=459770 DEFAULT CHARSET=utf8; 


DROP TABLE IF EXISTS `wp_posts`; 
CREATE TABLE `wp_posts` (
    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `post_author` bigint(20) unsigned NOT NULL DEFAULT '0', 
    `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `post_content` longtext NOT NULL, 
    `post_title` text NOT NULL, 
    `post_excerpt` text NOT NULL, 
    `post_status` varchar(20) NOT NULL DEFAULT 'publish', 
    `comment_status` varchar(20) NOT NULL DEFAULT 'open', 
    `ping_status` varchar(20) NOT NULL DEFAULT 'open', 
    `post_password` varchar(20) NOT NULL DEFAULT '', 
    `post_name` varchar(200) NOT NULL DEFAULT '', 
    `to_ping` text NOT NULL, 
    `pinged` text NOT NULL, 
    `post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `post_content_filtered` longtext NOT NULL, 
    `post_parent` bigint(20) unsigned NOT NULL DEFAULT '0', 
    `guid` varchar(255) NOT NULL DEFAULT '', 
    `menu_order` int(11) NOT NULL DEFAULT '0', 
    `post_type` varchar(20) NOT NULL DEFAULT 'post', 
    `post_mime_type` varchar(100) NOT NULL DEFAULT '', 
    `comment_count` bigint(20) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`ID`), 
    KEY `post_name` (`post_name`), 
    KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`), 
    KEY `post_parent` (`post_parent`), 
    KEY `post_author` (`post_author`) 
) ENGINE=InnoDB AUTO_INCREMENT=104855 DEFAULT CHARSET=utf8; 

INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES 
(104745, 0, '2014-02-15 00:00:00', '2014-02-15 00:00:00', '<div class=\"code-details\">\r\n<p class=\"code-description\"><span class=\"merchant-name\">Interflora</span> &pound;4 Off Orders over &pound;40 at Interflora T&C: Minimum product value of &pound;40 applies offer excludes overseas, giftcard and Interflora Gold purchases.</p>\r\n</div>', '&pound;4 Off Orders over &pound;40 at Interflora T&C: Minimum product value of &pound;40 applies offer excludes overseas, giftcard and Interflora Gold purchases.', '', 'publish', 'closed', 'closed', '', '4-off-orders-over-40-at-interflora-t40-applies-%c2%96-offer-excludes-overseas-giftcard-and-interflora-gold-purchases', '', '', '2014-02-17 16:01:36', '2014-02-17 16:01:36', '', 0, 'http://www.site.com/discount-codes/4-off-orders-over-40-at-interflora-t40-applies-%c2%96-offer-excludes-overseas-giftcard-and-interflora-gold-purchases/', 0, 'discount_codes', '', 0), 
(104744, 0, '2014-02-17 00:00:00', '2014-02-17 00:00:00', '<div class=\"code-details\">\r\n<p class=\"code-description\"><span class=\"merchant-name\">Interflora</span> 10% Off All Hampers over &pound;25 at Interflora T&C: Minimum product value of &pound;24.99 applies offer excludes overseas, giftcard and Interflora Gold purchases.</p>\r\n</div>', '10% Off All Hampers over &pound;25 at Interflora T&C: Minimum product value of &pound;24.99 applies offer excludes overseas, giftcard and Interflora Gold purchases.', '', 'publish', 'closed', 'closed', '', '10-off-all-hampers-over-25-at-interflora-t24-99-applies-%c2%96-offer-excludes-overseas-giftcard-and-interflora-gold-purchases', '', '', '2014-02-17 16:01:34', '2014-02-17 16:01:34', '', 0, 'http://www.site.com/discount-codes/10-off-all-hampers-over-25-at-interflora-t24-99-applies-%c2%96-offer-excludes-overseas-giftcard-and-interflora-gold-purchases/', 0, 'discount_codes', '', 0), 
(104743, 0, '2014-02-15 00:00:00', '2014-02-15 00:00:00', '<div class=\"code-details\">\r\n<p class=\"code-description\"><span class=\"merchant-name\">Interflora</span> 10% Off the Spring Flowers Collection at Interflora (Min Spend &pound;30) T&C: Minimum product value of &pound;30.00 applies offer excludes overseas, giftcard and Interflora Gold purchases.</p>\r\n</div>', '10% Off the Spring Flowers Collection at Interflora (Min Spend &pound;30) T&C: Minimum product value of &pound;30.00 applies offer excludes overseas, giftcard and Interflora Gold purchases.', '', 'publish', 'closed', 'closed', '', '10-off-the-spring-flowers-collection-at-interflora-min-spend-30-t30-00-applies-%c2%96-offer-excludes-overseas-giftcard-and-interflora-gold-purchases', '', '', '2014-02-17 16:01:33', '2014-02-17 16:01:33', '', 0, 'http://www.site.com/discount-codes/10-off-the-spring-flowers-collection-at-interflora-min-spend-30-t30-00-applies-%c2%96-offer-excludes-overseas-giftcard-and-interflora-gold-purchases/', 0, 'discount_codes', '', 0), 
(104666, 0, '2014-02-17 00:00:00', '2014-02-17 00:00:00', '<div class=\"code-details\">\r\n<p class=\"code-description\"><span class=\"merchant-name\">Marks and Spencer</span> 20% off Outlet (does not include the BOGOHP offers)</p>\r\n</div>', '20% off Outlet (does not include the BOGOHP offers)', '', 'publish', 'closed', 'closed', '', '20-off-outlet-does-not-include-the-bogohp-offers', '', '', '2014-02-17 16:01:50', '2014-02-17 16:01:50', '', 0, 'http://www.site.com/discount-codes/20-off-outlet-does-not-include-the-bogohp-offers/', 0, 'discount_codes', 




INSERT INTO `wp_postmeta` (`meta_id`, `post_id`, `meta_key`, `meta_value`) VALUES 
(459207, 104743, 'discount_code_merchant_name', 'Interflora'), 
(459208, 104743, 'discount_code', 'SPINGFLO'), 
(459209, 104743, 'discount_code_url', 'd.php?v=1969&amp;t=141617&amp;p=http://www.interflora.co.uk/category/spring-flowers'), 
(459210, 104743, 'discount_code_start_date', '2014-02-05 00:00:00'), 
(459211, 104743, 'discount_code_end_date', '2014-02-08 23:59:59'), 
(459212, 104743, '_wp_page_template', 'default’), 


(459213, 104744, 'discount_code_merchant_name', 'Interflora'), 
(459214, 104744, 'discount_code', 'HAMPER'), 
(459215, 104744, 'discount_code_url', 'd.php?v=1969&amp;t=141617&amp;p=http://www.interflora.co.uk/category/hampers-gifts/'), 
(459216, 104744, 'discount_code_start_date', '2014-02-17 00:00:00'), 
(459217, 104744, 'discount_code_end_date', '2014-02-23 23:59:59'), 
(459218, 104744, '_wp_page_template', 'default'), 
(459225, 104745, 'discount_code_merchant_name', 'Interflora'), 
(459226, 104745, 'discount_code', 'SPRING4IT'), 
(459227, 104745, 'discount_code_url', 'd.php?v=1969&amp;t=141617&amp;p=http://www.interflora.co.uk/'), 
(459228, 104745, 'discount_code_start_date', '2014-02-15 00:00:00'), 
(459229, 104745, 'discount_code_end_date', '2014-02-28 23:59:59'), 
(459230, 104745, '_wp_page_template', 'default’), 


(459285, 104666, 'discount_code_merchant_name', 'Marks and Spencer'), 
(459286, 104666, 'discount_code', 'N/A'), 
(459287, 104666, 'discount_code_url', 'd.php?v=1402&amp;t=141617&amp;p=http://outlet.marksandspencer.com/'), 
(459288, 104666, 'discount_code_start_date', '2014-03-17 00:00:00'), 
(459289, 104666, 'discount_code_end_date', '2014-03-18 23:59:59'), 
(459290, 104666, '_wp_page_template', 'default’); 
+0

질문에 모든 검색어를 추가하는 대신 http://sqlfiddle.com/에서 테이블 정의를 추가하는 것이 더 좋습니다. –

답변

1

당신은 우리에게 약간의 리버스 엔지니어링을 해 줬습니다! 당신이 필요로하는 무엇

, 나는 생각 pseudosql에,

SELECT id 
    FROM discount_codes 
    WHERE discount_code_start_date < NOW() 
    AND discount_code_end_date > NOW() 

이 다시 시작했지만 종료하지 않은 할인 코드에 대한 포스트 ID 목록을 걷어 것입니다.

그래서 트릭은 어떻게 든 WordPress 스키마를 강요하여 결과를 얻는 것입니다. 늘 그렇듯이, 그것을 이해하는 재미있는 양동이입니다.

다음은 작업 내용입니다. 활성 할인 코드 인 wp_posts 항목 id 값을 가져와야합니다. 그 정도로 간단합니다.

SELECT id 
    FROM wp_posts 
WHERE post_type = 'discount_codes' 
    AND post_status = 'publish 

다음으로 유용한 post_meta 항목을 가져와야합니다. 이들은 하위 쿼리를 사용하여 올바르게 수행합니다.

SELECT post_id AS id, 
     CAST(meta_value AS DATETIME) AS discount_code_start_date 
    FROM post_meta 
WHERE meta_key = 'discount_code_start_date' 

마찬가지로 종료 날짜가 필요합니다.

SELECT post_id AS id, 
     CAST(meta_value AS DATETIME) AS discount_code_end_date 
    FROM post_meta 
WHERE meta_key = 'discount_code_end_date' 

그런 다음이 모든 것을 함께 결합하고 올바른 WHERE 절을 적용해야합니다.

SELECT p.id 
    FROM wp_posts AS p 
    JOIN (
     SELECT post_id AS id, 
       CAST(meta_value AS DATETIME) AS discount_code_start_date 
     FROM post_meta 
     WHERE meta_key = 'discount_code_start_date' 
     ) AS s ON (p.id = s.id) 
    JOIN (
     SELECT post_id AS id, 
      CAST(meta_value AS DATETIME) AS discount_code_end_date 
     FROM post_meta 
     WHERE meta_key = 'discount_code_end_date' 
     ) AS e ON (p.id = e.id) 
WHERE p.post_type = 'discount_codes' 
    AND p.post_status = 'publish' 
    AND s.discount_code_start_date < NOW() 
    AND e.discount_code_end_date > NOW() 

post_meta 테이블을 올바르게 사용하는 비결은 적절한 meta_key 값으로 행을 가져 오는 하위 쿼리에 있습니다.

+0

화려한 - 매우 간결한 - 많은 감사합니다. – user1745467