草庐IT

php - 拆分日期范围并在 mysql 中获得合并结果

coder 2023-10-16 原文

我有两张 table

订单状态历史

   id | order_id | status |   date 
   1  |  1201    |  2     | 2015-01-20
   2  |  1124    |  4     | 2015-02-01
   3  |  1245    |  1     | 2015-02-14
   4  |  1365    |  2     | 2015-03-10

saved_shipping_invoices

   id | order_id  | product_id |  date 
   1  |  1348     |  12541     | 2015-12-18
   2  |  1298     |  11485     | 2016-01-02
   3  |  1319     |  14521     | 2016-05-14
   4  |  1441     |  10124     | 2016-05-14

和一年的日期范围即

$start_date = '2015-09-30';
$end_date   = '2016-09-30';

“shipping_invoice_history”表中的数据从 2015 年 12 月 18 日开始。现在我想打破 2015-12-18 的日期间隔。现在会有两个间隔

$interval1_start = '2015-09-30';
$interval_end    = '2015-12-17';

$interval2_start = '2015-12-18';
$interval2_end   = '2016-09-30';

当它打破日期间隔时,我想在第一个间隔从表“orders_status_history”获取数据,但在第二个间隔从“shipping_invoice_history”获取数据。我还需要在单个表中显示数据。

  $week = '2015-09-30 @@ 2016-09-30';
     $week_ranges  = explode('@@', $week);
    if ($week_ranges[0] > '2015-12-18' )
                    {
                        $invoices_shipped_qty  = tep_db_query("SELECT  *  FROM `saved_shipping_invoices` WHERE `date_created` BETWEEN '".$week_ranges[0]."' AND '".$week_ranges[1]."'");
                        $invoices_shipped_arr = tep_db_fetch_array($invoices_shipped_qty);
                        $total_sent =  $invoices_shipped_arr['shiped_qty'];
                        }
                        else {  
                    $total_order_status_complete = tep_db_query("SELECT  *  FROM `orders_status_history` WHERE `orders_status_id` = '17'  AND `date_added` BETWEEN '".$week_ranges[0]." 00:00:00' AND '".$week_ranges[1]." 23;59:59'");
                    $total_order_status_complete_arr = tep_db_fetch_array($total_order_status_complete);

请建议我如何将日期间隔分成两部分,从两个表中获取结果并在单次运行中显示。

最佳答案

可能这就是你想要的

select 
  'order_status_history' as table, 
  id, 
  order_id, 
  status, 
  null as product_id, 
  date from order_status_history
where 
 date between '2015-09-30' and '2015-12-17'
union all
select 
  'saved_shipping_invoices' as table, 
  id,
  order_id,
  null as status, 
  product_id, 
  date 
from saved_shipping_invoices
 where 
  date between '2015-12-18' and '2016-09-30' 

关于php - 拆分日期范围并在 mysql 中获得合并结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39786349/

有关php - 拆分日期范围并在 mysql 中获得合并结果的更多相关文章

  1. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  2. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  3. ruby - 触发器 ruby​​ 中 3 点范围运算符和 2 点范围运算符的区别 - 2

    请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是

  4. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  5. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  6. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  7. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  8. ruby - 检查日期是否在过去 7 天内 - 2

    我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/

  9. ruby - 当使用::指定模块时,为什么 Ruby 不在更高范围内查找类? - 2

    我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or

  10. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

随机推荐