【问题标题】:MySQL Query with Idiorm and Date Issue带有惯用语和日期问题的 MySQL 查询
【发布时间】:2014-07-29 07:55:42
【问题描述】:

我想在我的一些数据上使用图表并根据所选日期生成值(使用日期选择器)。我在网上偷了大部分东西,可能只有一个简单的问题。

这是惯用语查询:

if (isset($_GET['start']) AND isset($_GET['end'])) {

$start = $_GET['start'];
$end = $_GET['end'];
$data = array();

// Select the results with Idiorm
$results = ORM::for_table('tbl_data')
        ->where_gte('date', $start)
        ->where_lte('date', $end)
        ->order_by_desc('date')
        ->find_array();


// Build a new array with the data
foreach ($results as $key => $value) {
    $data[$key]['label'] = $value['date'];
    $data[$key]['value'] = $value['rev'];
}

echo json_encode($data);
}

$start$end 来自我的日期选择器,格式为 yyyy-mm-dd。我唯一不知道该怎么做是如何更改->where_gte 语句。如您所见,它正在数据库中查询字段date。在我的数据库中,我有三个字段,yearmonthday

有没有办法将yearmonthday 这三个字段组合成一个表达式,即可能是->where_gte('year'&'month'&'day', $start)???

我尝试搜索和搜索,但可能有错误的关键字或知识较少。

提前感谢您的帮助!

【问题讨论】:

    标签: php mysql sql date idiorm


    【解决方案1】:

    由于数据库中有三个字段,因此需要三个 where_gte 子句:

    ...
    ->where_gte('year', substr($start, 0, 4) // or most suitable date_format
    ->where_gte('month', substr($start, 5, 2) // or most suitable date_format
    ...
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      您可以使用 MySql DATE_FORMAT() 函数管理日期格式。例如:

      // Select the results with Idiorm
      $results = ORM::for_table('tbl_data')
              ->where_gte('date', "DATE_FORMAT($start, '%Y-%m-%d')")
              ->where_lte('date', "DATE_FORMAT($end, '%Y-%m-%d')")
              ->order_by_desc('date')
              ->find_array();
      

      【讨论】:

      • 谢谢 - 但我的 sql 表中没有日期字段。我正在寻找的是根据我所拥有的年、月和日的值创建一个日期。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      相关资源
      最近更新 更多