作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
select
x1.category
, x1.title
from yourtable x1
where
(
select count(*)
from yourtable x2
where x2.category = x1.category
and x2.title <= x1.title
) <= 2
order by category, title;
我的模型中有类似的东西。
$this->db->select('x1.category','x1.title, false);
$this->db->from('yourtable as x1');
$sub = $this->subquery->start_subquery('where');
$sub ->select('count(*) ')<=25;
$sub ->from('yourtable as x2');
$sub ->where('x2.category = x1.category and x2.title<= x1.title');
$this->subquery->end_subquery('count(*)');
$this->db->order_by('category','title','RANDOM');
$query = $this->db->get('yourtable');
我收到错误
Fatal error: Call to a member function start_subquery() on a non-object in ....
最佳答案
您需要为 codeigniter 设置子查询:
http://labs.nticompassinc.com/CodeIgniter-Subqueries/
此外,您可以这样做:
$this->db->select('x1.category','x1.title', false);
$this->db->from('yourtable as x1');
$this->db->where('(SELECT count(*) FROM yourtable x2 WHERE x2.category = x1.category AND x2.title <= x1.title) <= 2', NULL, false);
$this->db->order_by('category','title');
关于php - 如何将 Sql 代码转换为 codeigniter 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184289/
我是一名优秀的程序员,十分优秀!