获取包含另一个表中的一些数据的列表用户

发布于 2024-11-19 12:42:03 字数 443 浏览 0 评论 0原文

我有表(用户) 我还有另一个表(书籍)

用户表:

Id |用户名

1 :: john

2 :: doe

books 表:

id |书名 | user_id(拥有这本书的用户)

1 :: book1 :: 2

2 :: book2 :: 2

3 :: book3 :: 2

4 :: book4 :: 1

5 :: book5 :: 1

现在,我如何获取列表用户数量并获取每个用户拥有多少本书,

如下所示:

  • john(2 本书)

  • doe(3 本书)


我使用 codeigniter 框架..

提前致谢:)

I have table ( users )
and I have another table let's say (books)

users table :

Id | Username

1 :: john

2 :: doe

books table:

id | book_title | user_id(user who own this book)

1 :: book1 :: 2

2 :: book2 :: 2

3 :: book3 :: 2

4 :: book4 :: 1

5 :: book5 :: 1

Now , how can I get list of users and get how many books every user have

like this :

  • john ( 2 books )

  • doe ( 3 books )


I use codeigniter framework ..

thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦途 2024-11-26 12:42:03

回答你的第二个问题:

我只需要为我自己的一些分析执行此操作:

SELECT users.* , 

   (SELECT COUNT( 1 ) 
    FROM books
    WHERE user_id = users.id) 
    AS  "Number of Books",

   (SELECT COUNT( 1 )
    FROM files
    WHERE user_id = users.id)
    AS  "Number of Files"

FROM users

如果您必须经常执行此操作,您可能想弄清楚如何使用比子查询更有效的多个连接来执行此操作。

To answer your second question:

I just had to do this for some of my own analytics:

SELECT users.* , 

   (SELECT COUNT( 1 ) 
    FROM books
    WHERE user_id = users.id) 
    AS  "Number of Books",

   (SELECT COUNT( 1 )
    FROM files
    WHERE user_id = users.id)
    AS  "Number of Files"

FROM users

If you have to do this frequently, you might want to figure out how to do this using multiple joins which are more efficient than subqueries.

黑色毁心梦 2024-11-26 12:42:03

这是SQL数据库吗?你不能使用 SQL 查询来得到你想要的吗?:

SELECT Username, COUNT(books) AS how_many_books FROM users JOIN books ON users.id=books.user_id GROUP BY Username

Is this SQL database? Couldn't you use SQL query to get what you want?:

SELECT Username, COUNT(books) AS how_many_books FROM users JOIN books ON users.id=books.user_id GROUP BY Username
℡Ms空城旧梦 2024-11-26 12:42:03

在循环访问用户的输出时,您需要使用 $this->db->count_all_results();在循环内再次调用数据库其中行选择用户ID。抱歉没有解释清楚,但如果你理解它的逻辑,那就没问题了。

但这是我的解释,没有太多关于 JOIN 的知识,如上所示。

While looping through your output of users, you'll want to make another call to the DB within the loop with something like $this->db->count_all_results(); with a where line to select the userID. Sorry for not explaining too well, but if you understand the logic of it, you'll be fine.

BUT this is my explanation without too much knowledge of JOIN, as seen above.

恏ㄋ傷疤忘ㄋ疼 2024-11-26 12:42:03

如果你使用 CI 的 Active Record 框架,它会是这样的:

$this->db->select('Users.Username, COUNT(Book.Id) AS user_book_count');
$this->db->from('Users');
$this->db->join('Books', 'Users.Id = Books.user_id', 'left');
$this->db->group_by('Users.Id');

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

if ($query->num_rows() > 0)
{
    foreach ($query->result() as $row) {
        $users[] = $row->username . '(' . $row->user_book_count . ')';
    }
    return $users;
}

If you're using CI's Active Record framework, it would be something like this:

$this->db->select('Users.Username, COUNT(Book.Id) AS user_book_count');
$this->db->from('Users');
$this->db->join('Books', 'Users.Id = Books.user_id', 'left');
$this->db->group_by('Users.Id');

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

if ($query->num_rows() > 0)
{
    foreach ($query->result() as $row) {
        $users[] = $row->username . '(' . $row->user_book_count . ')';
    }
    return $users;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文