gpt4 book ai didi

日期范围之间的mysql变量避免重复

转载 作者:行者123 更新时间:2023-11-29 14:01:33 27 4
gpt4 key购买 nike

我不确定是否可能,但我可以使用 set vars 来避免在以下查询中重复日期范围吗?所以我只会声明一次 TY 和 LY 并在查询中重复使用?

// ThisYear -- '2013-02-04 00:00:00' AND '2013-02-10 23:59:59'
// LastYear -- '2012-02-06 00:00:00' AND '2013-02-12 23:59:59'

SELECT
count(*) as total_num_bookings,

SUM(if(book_date BETWEEN '2013-02-04 00:00:00' AND '2013-02-10 23:59:59', 1, 0)) AS num_bookings_ty,
SUM(if(book_date BETWEEN '2012-02-06 00:00:00' AND '2012-02-12 23:59:59', 1, 0)) AS num_bookings_ly,
sj.pu_country

FROM
pc_tech.sales_journal_default sj

WHERE (book_date BETWEEN '2013-02-04 00:00:00' AND '2013-02-10 23:59:59') OR (book_date BETWEEN '2012-02-06 00:00:00' AND '2012-02-12 23:59:59' )
group by pu_country
order by total_num_bookings DESC
LIMIT 25;

最佳答案

是的:

SET @ThisYear1='2013-02-04 00:00:00', @ThisYear2='2013-02-10 23:59:59'
SET @LastYear1='2012-02-06 00:00:00', @LastYear2='2013-02-12 23:59:59'

SELECT
count(*) as total_num_bookings,

SUM(if(book_date BETWEEN @ThisYear1 AND @ThisYear2, 1, 0)) AS num_bookings_ty,
...

编辑

或者使用准备好的语句:

SET @ThisYear="'2013-02-04 00:00:00' AND '2013-02-10 23:59:59'";
SET @LastYear="'2012-02-06 00:00:00' AND '2013-02-12 23:59:59'";

SET @sql = CONCAT("SELECT count(*) as total_num_bookings, SUM(if(book_date BETWEEN ", @ThisYear, ", 1, 0)) AS num_bookings_ty, ...");
PREPARE stmt from @sql;
EXECUTE stmt;
...
DEALLOCATE PREPARE stmt;

关于日期范围之间的mysql变量避免重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985088/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com