$query = $wpdb->prepare("SELECT post_id
FROM {$wpdb->prefix}postmeta t1
WHERE meta_key = 'expiry_date'
AND STR_TO_DATE(meta_value, '%Y-%m-%d') BETWEEN NOW() AND NOW() + INTERVAL 6 MONTH
AND EXISTS (SELECT 1 FROM wp_postmeta t2
WHERE t2.meta_key = 'customer_email'
AND t2.meta_value = %s
AND t2.post_id = t1.post_id)
GROUP BY post_id", 'test@hotmail.co.uk');
$result = $wpdb->get_results( $query, ARRAY_A );
$result返回空,原因如下:
AND STR_TO_DATE(meta_value, '%Y-%m-%d') BETWEEN NOW() AND NOW() + INTERVAL 6 MONTH
meta_value列将日期保存为字符串,因此我需要使用STR_TO_DATE。
当我在phpmyadmin中运行这个查询时,它完全工作,但在我的php wordpress文件中不工作。当我删除上面的代码行时,它只在我的php文件中工作。
1条答案
按热度按时间x8diyxa71#
@anyber使用“%%Y-%%m-%%d”解决了此问题。谢谢