codeigniter - 活动纪录 - SQL - 复加盟 [英] Codeigniter - Active record - sql - complex join

查看:113
本文介绍了codeigniter - 活动纪录 - SQL - 复加盟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,检索所有的标签从表中:

I have a function that retrieves all tags from a table:

function global_popular_tags() {
    $this->db->select('tags.*, COUNT(tags.id) AS count');
    $this->db->from('tags');
    $this->db->join('tags_to_work', 'tags.id = tags_to_work.tag_id');
    $this->db->group_by('tags.id');
    $this->db->order_by('count', 'desc'); 
    $query = $this->db->get()->result_array();
    return $query;
}

我还有一个表称为工作。在工作表中有一个草案一栏为0或1。我想COUNT(tags.id)值,考虑到与特定标签的工作是否处于草稿模式(1)或不。

I have another table called 'work'. The 'work' table has a 'draft' column with values of either 1 or 0. I want the COUNT(tags.id) to take into account whether the work with the specific tag is in draft mode (1) or not.

说,有10件作品的标记,例如,设计。计数为10。但是,其中2件作品都在草稿模式下,因此计数确实应该8.如何管理呢?

Say there are 10 pieces of work tagged with, for example, 'design'. The COUNT will be 10. But 2 of these pieces of work are in draft mode, so the COUNT should really be 8. How do I manage this?

推荐答案

尝试改变:

$this->db->from('tags');
$this->db->join('tags_to_work', 'tags.id = tags_to_work.tag_id');

要:

$this->db->from('tags, work');
$this->db->join('tags_to_work', 'tags.id=tags_to_work.tag_id AND work.id=tags_to_work.work_id');

和添加:

$this->db->where('work.drafts', 0);

这篇关于codeigniter - 活动纪录 - SQL - 复加盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆