【问题标题】:CI3 - Count and Joins in Active RecordCI3 - Active Record 中的计数和联接
【发布时间】:2019-03-12 23:58:32
【问题描述】:

我就是无法让这个工作。我有一个像这样的类别表:

table name:  categories
id
name

还有一个像这样的帖子表:

table name: posts
id
category_id
title
body

我正在编写一个控制器/模型/视图来列出 category.name 和属于 at 类别的帖子数。我正在崩溃和燃烧如何做到这一点。这是部分尝试。

$this->db->select('
                posts.*,
                categories.*,
                COUNT(posts.category_id) as num_posts
            ');
$this->db->from('posts');
$this->db->join('categories', 'categories.id = posts.category_id');
$xx = $this->db->get();

    echo '<pre>';
    print_r($xx);
    echo '</pre>';
    exit();

非常感谢任何帮助。已经搜索了我的 stackoverflow,但看不到 CI3 解决方案。

新的更新。此 sql 有效:

SELECT
    categories.id,
    categories.name,
    Count(posts.id) as cnt
FROM
    categories
LEFT JOIN
    posts
ON
    categories.id=posts.category_id
GROUP BY
    categories.name

当我运行它时,我得到的结果低于我的预期:

id     name                              cnt
---------------------------------------------
80  |  24354234234                    |  2
76  |  Associations and Organizations |  9

但是当我像这样更改为活动记录时:

$this->db->select('categories.id, categories.name, COUNT(posts.id) as num_posts');
$this->db->from('categories');
$this->db->join('posts', 'categories.id = posts.category_id');
$this->db->group_by('categories.name');
$xx = $this->db->get();

我 print_r($xx) 没有看到我的任何数据。这是我得到的:

CI_DB_mysqli_result Object
(
[conn_id] => mysqli Object
    (
        [affected_rows] => 2
        [client_info] => 5.5.59
        [client_version] => 50559
        [connect_errno] => 0
        [connect_error] => 
        [errno] => 0
        [error] => 
        [error_list] => Array
            (
            )

        [field_count] => 3
        [host_info] => Localhost via UNIX socket
        [info] => 
        [insert_id] => 0
        [server_info] => 5.5.58-0ubuntu0.14.04.1
        [server_version] => 50558
        [stat] => Uptime: 7546485  Threads: 2  Questions: 523149  Slow queries: 0  Opens: 257  Flush tables: 1  Open tables: 200  Queries per second avg: 0.069
        [sqlstate] => 00000
        [protocol_version] => 10
        [thread_id] => 42310
        [warning_count] => 0
    )

[result_id] => mysqli_result Object
    (
        [current_field] => 0
        [field_count] => 3
        [lengths] => 
        [num_rows] => 2
        [type] => 0
    )

[result_array] => Array
    (
    )

[result_object] => Array
    (
    )

[custom_result_object] => Array
    (
    )

[current_row] => 0
[num_rows] => 
[row_data] => 
)

仍然不知道如何获取数组。任何帮助表示赞赏。

【问题讨论】:

  • $this-&gt;db-&gt;select('categories.*,COUNT(posts.posts.id) as num_posts'); 怎么样?
  • 谢谢。它给出和错误。错误号:1051 Unknown table 'categories' SELECT categories.*, COUNT(posts.posts.id) as num_posts 文件名:models/Blog_model.php 行号:68
  • 是的,好吧-现在想想-这是什么意思? ;)
  • 谢谢。我可以看到结果对象可能有数据? 3 个字段和 2 行。 [result_id] => mysqli_result 对象([current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 2 [type] => 0)
  • 我如何访问它?

标签: php sql codeigniter activerecord codeigniter-3


【解决方案1】:

我发现这解释了为什么这些值是空白的:Codeigniter query returning blank

更多文档在这里。 https://www.codeigniter.com/user_guide/database/results.html

因此,这将回显这些值:

... extension of previous code
$xx = $this->db->get();
print_r($xx->result_array());

【讨论】:

    猜你喜欢
    • 2017-10-14
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多