codeigniter sql查询构建

3bygqnnd  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(333)

我无法将以下sql查询转换为codeigniter查询生成器:

$row = $db->prepare("SELECT DATE(message_timestamp) Date, COUNT(DISTINCT messageid) totalCount FROM ic_deliveryreceipts GROUP BY DATE(message_timestamp)");

这是我最近尝试的:

$this->db->select(DATE(message_timestamp) as 'Date', COUNT(DISTINCT messageid) as 'totalCount');
    $this->db->group_by(DATE(message_timestamp));
    $query = $this->db->get($this->ic_receipts);

欢迎帮忙!谢谢您

pobjuy32

pobjuy321#

我认为您需要引号,并在中的第二个(可选)参数上加上false $this->db->select ,如codeigniter文档所示:
$this->db->select()接受可选的第二个参数。如果将其设置为false,codeigniter将不会尝试保护字段或表名。如果您需要一个复合select语句,而字段的自动转义可能会打断它们,那么这将非常有用。
试着这样写:

$this->db->select("DATE(message_timestamp) as 'Date', 
                   COUNT(DISTINCT messageid) as 'totalCount'"
                  , FALSE);

$this->db->group_by("DATE(message_timestamp)");

$query = $this->db->get($this->ic_receipts);
c2e8gylq

c2e8gylq2#

这就是我最终得出的结论:

$this->db->select('DATE(message_timestamp) Date, COUNT(DISTINCT messageid) totalCount');
$this->db->from('ic_deliveryreceipts');
$this->db->group_by('DATE(message_timestamp)');
$query = $this->db->get();

谢谢你的建议

jfewjypa

jfewjypa3#

$row = $this->db
              ->query("SELECT DATE(message_timestamp) Date, 
                       COUNT(DISTINCT messageid) totalCount 
                       FROM ic_deliveryreceipts 
                       GROUP BY DATE(message_timestamp)")
              ->row();
``` `$row` 是具有两个属性的stdclass对象: `Date` ,  `totalCount` ```
echo $row->Date . " " . $row->totalCount;

相关问题