我有一个wordpress网站,我必须通过外部脚本将信息输入外部应用程序。
我只是在一次mysql调用上遇到一点麻烦
我在wp_posts表中列出了折扣,然后是wp_postmeta表中的开始和结束日期。
但是,我可以在一个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> £4 Off Orders over £40 at Interflora T&C: Minimum product value of £40 applies offer excludes overseas, giftcard and Interflora Gold purchases.</p>\r\n</div>', '£4 Off Orders over £40 at Interflora T&C: Minimum product value of £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 £25 at Interflora T&C: Minimum product value of £24.99 applies offer excludes overseas, giftcard and Interflora Gold purchases.</p>\r\n</div>', '10% Off All Hampers over £25 at Interflora T&C: Minimum product value of £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 £30) T&C: Minimum product value of £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 £30) T&C: Minimum product value of £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&t=141617&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&t=141617&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&t=141617&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&t=141617&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 :(得分: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值拉取行的子查询。