【问题标题】:How can I write the following query in CodeIgniter? [closed]如何在 CodeIgniter 中编写以下查询? [关闭]
【发布时间】:2015-11-04 15:59:56
【问题描述】:

我正在使用 CodeIgniter 开发一个应用程序,但我无法使用活动记录编写以下查询,

select price, count(*) from fake_apps where downloads > 20000 group by price;

如何使用 CodeIgniter 的活动记录编写此查询?

谢谢。

【问题讨论】:

  • 你想做什么?你的代码的目的是什么?

标签: mysql sql codeigniter codeigniter-3


【解决方案1】:
$this->db->select("price,count(*)",FALSE);
$this->db->from("fake_apps");
$this->db->where("downdoad>","20000");
$this->db->group_by("price);
$res = $this->db->get();

【讨论】:

  • 如果我们在 select 中不使用 FALSE 会不会影响查询?
  • 省略,codeigniter 在count(*) 周围加上引号,但count(*) 不是表格的字段
【解决方案2】:

应该是这样的:

$this->db->select("price, count(*)",FALSE);
$this->db->where("downdoad > ","20000");
$this->db->group_by("price");
$sql = $this->db->get("fake_apps");

echo '<pre>';
print_r($this->db->last_query());
echo '</pre>';

对于 Codeigniter 中的FALSE 选择:

$this->db->select("price, count(*)",FALSE);

注意:

如果您将其设置为 FALSE,CodeIgniter 将不会尝试保护您的字段 或带有反引号的表名。如果您需要化合物,这很有用 选择语句。

参考:

【讨论】:

  • 如果我们在 select 中不使用 FALSE 会不会影响查询?
猜你喜欢
  • 2016-05-03
  • 2021-11-07
  • 2021-09-06
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 2014-01-23
  • 2012-01-19
  • 1970-01-01
相关资源
最近更新 更多