2017-04-10 8 views
1

次のクエリでcompleted_dateをフィルタリングするにはどうすればよいですか? 2017年2月25日と2017年2月28日カスタマイズクエリをフィルタリングする方法は?

order_id,order_currency,order_total,completed_date 
5333,USD,299.00,"2017-02-28 17:03:05" 
5332,USD,15.00,"2017-02-25 12:04:10" 
5330,USD,15.00,"2017-02-25 01:50:07" 
5329,USD,129.00,"2017-02-25 01:48:36" 

、別の質問はどのようにできた」である間に、フィルタ後の

SELECT `wp_woocommerce_order_items`.order_id, 
MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_order_currency' THEN `wp_postmeta`.`meta_value` END) AS order_currency, 
MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_order_total' THEN `wp_postmeta`.`meta_value` END) AS order_total, 
MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_completed_date' THEN `wp_postmeta`.`meta_value` END) AS completed_date 
FROM wp_posts 
LEFT JOIN `wp_woocommerce_order_items` ON `wp_posts`.`ID`= `wp_woocommerce_order_items`.`order_id` AND order_item_type = 'line_item' 
LEFT JOIN `wp_postmeta` ON `wp_postmeta`.`post_id` = `wp_posts`.`ID` 
LEFT JOIN `wp_woocommerce_order_itemmeta` ON `wp_woocommerce_order_itemmeta`.order_item_id = `wp_woocommerce_order_items`.order_item_id 
WHERE `wp_posts`.`post_status` = 'wc-completed' 
GROUP BY `wp_woocommerce_order_items`.order_item_id 
ORDER BY completed_date DESC 

更新

フィルタ前

order_id,order_currency,order_total,completed_date 
5338,USD,299.00,"2017-03-09 10:07:00" 
5336,USD,15.00,"2017-03-04 03:54:41" 
5335,USD,299.00,"2017-03-03 17:34:19" 
5334,USD,15.00,"2017-03-01 12:30:54" 
5333,USD,299.00,"2017-02-28 17:03:05" 
5332,USD,15.00,"2017-02-25 12:04:10" 
5330,USD,15.00,"2017-02-25 01:50:07" 
5329,USD,129.00,"2017-02-25 01:48:36" 
5328,USD,299.00,"2017-02-23 23:55:36" 
5327,USD,39.00,"2017-02-23 16:06:00" 
5326,USD,299.00,"2017-02-21 19:33:43" 
5325,USD,15.00,"2017-02-19 15:35:34" 

同じ結果を得るためにこのクエリを変更しますか? " 5秒以上かかるためです。

+0

、あなたが取得したいつながるものをご提示ください。 – Palindromer

+0

@Palindromer、私はresultで私の質問を更新しました。 –

答えて

0

空腹のために添え字wp_posts。どこ結果についてpost_status

:次に

SELECT `wp_woocommerce_order_items`.order_id, 
MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_order_currency' THEN 
`wp_postmeta`.`meta_value` END) AS order_currency, 
MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_order_total' THEN 
`wp_postmeta`.`meta_value` END) AS order_total, 
MAX(CASE WHEN `wp_postmeta`.`meta_key` = '_completed_date' THEN 
`wp_postmeta`.`meta_value` END) AS completed_date 
into #tempTable 
FROM wp_posts 
LEFT JOIN `wp_woocommerce_order_items` ON `wp_posts`.`ID`= 
`wp_woocommerce_order_items`.`order_id` AND order_item_type = 
'line_item' 
LEFT JOIN `wp_postmeta` ON `wp_postmeta`.`post_id` = `wp_posts`.`ID` 
LEFT JOIN `wp_woocommerce_order_itemmeta` ON 
`wp_woocommerce_order_itemmeta`.order_item_id = 
`wp_woocommerce_order_items`.order_item_id 
WHERE `wp_posts`.`post_status` = 'wc-completed' 
    GROUP BY `wp_woocommerce_order_items`.order_item_id 
    ORDER BY completed_date DESC 

declare @date1,date2 
    set date1 ='2017-02-25 00:00:00' 
    set date2 ='2017-02-28 00:00:00' 

select * from #tempTable 
where completed_date>date1 and completed_date <=date2 

drop #tmpTable 
関連する問題