Codeigniter按价格和当前字段查询顶级商店

时间:2019-06-20 02:12:56

标签: codeigniter

我是codeigniter的初学者。 我有这样的查询,我想在codeigniter中使用此查询 我想按total_price来获得顶级store_name

+---+------------+-------------+-------------+---------------------+
| id| store_name |  quality    | total_price |     time            |
+---+------------+-------------+-------------+---------------------+
| 1 | store_a    |     2       |   300       | 2019-06-19 08:20:57 | 
| 2 | store_b    |     1       |   100       | 2019-06-20 08:30:57 |
| 3 | store_c    |     4       |   50        | 2019-06-20 08:33:57 |
| 4 | store_b    |     2       |   300       | 2019-06-20 08:35:57 |
+---+------------+-------------+-------------+---------------------+

我尝试过

$this->db->select('store_name, SUM(total_price) AS  t_price', false);
$this->db->group_by('store_name');
$query = $this->db->get($this->table_name);

return $query->result();

我今天需要按日期时间输出

    +------------+-------------+-------------+---------------------+
    | store_name |  quality    | total_price |     time            |
    +------------+-------------+-------------+---------------------+
    | store_b    |     3       |   400       | 2019-06-20 08:30:57 |
    | store_c    |     4       |   50        | 2019-06-20 08:33:57 |

更新 为我工作

    $date = new DateTime("now");
    $curr_date = $date->format('Y-m-d ');

    $this->db->select('store_name, SUM(total_price) AS  total_price ,SUM(quality) AS    quality', false);
    $this->db->group_by('store_name');
    $this->db->where('DATE(time)',$curr_date);
    $this->db->order_by('total_price', "DESC");
    $query = $this->db->get($this->table_name);


    return $query->result();

1 个答案:

答案 0 :(得分:0)

您可以尝试解决此问题的方法:

$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');

$this->db->select('store_name, SUM(total_price) AS  total_price ,SUM(quality) AS    quality', false);
$this->db->where('DATE(time)',$curr_date);
$this->db->group_by('store_name');
$this->db->order_by('total_price', "DESC");
$query = $this->db->get($this->table_name);


return $query->result();

它将对您有所帮助。