【问题标题】:SQL join in CodeIgniter with Active Record使用 Active Record 在 CodeIgniter 中加入 SQL
【发布时间】:2012-02-17 18:22:44
【问题描述】:

我正在努力解决这个问题,但我似乎在绕圈子。我正在尝试一一列出用户主题,下面是属于该特定主题的引号。如果这有意义的话。

我有 3 张桌子,如下所示:

[USERS] user_id 用户名

[主题] 主题 ID 用户 ID 主题名称

[QUOTES] quote_id topic_id quote_name

在我看来,我希望能够做这样的事情:

用户名:托马斯

主题 1:随便

引用:一个引用,另一个引用和第三个引用,都属于主题 1。

主题 2:Thomas 的另一个主题

引用:是的,好的,谢谢,我喜欢 Stack Overflow,这些引用属于主题 2。

但我无法让它工作,我一直在尝试一切,包括奇怪的东西,例如:

public function get_quotes()
{

    $this->db->select('*');
    $this->db->from('topics');
    $this->db->join('quotes', 'topic_id = quote_id');

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

    if($query->num_rows() > 0)
    {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
    }
    return $data;
}

这很奇怪,我应该改用“where”吗?类似的东西:

$this->db->where('user', $user_id);
$this->db->where('topic', $topic_id);
$this->db->where('quote', $quote_id);

我真的很感谢我能得到的任何帮助,或者只是一个指向正确方向的手指!

【问题讨论】:

    标签: php codeigniter activerecord


    【解决方案1】:

    马上我会问“什么不起作用?”,其次我建议你运行分析器来显示正在生成的EXACT SQL,所以您可以对 ACTIVE QUERY 失败的地方做出有效评估。

    要使用分析器,请将其粘贴到您的控制器中:

    $this->output->enable_profiler(TRUE);
    

    这将导致所有数据库调用、所有 POST 变量等的良好输出;
    参考这里:http://codeigniter.com/user_guide/libraries/output.html

    更新

    所以要完全做你想做的事,你需要一个返回以下列的查询:

    user_id, username, topic_id, topic_name, quote_id, quote_name
    

    这是您想要的活动查询(如果足够清楚,您也可以使用方法链接):

    $this->db->select('u.user_id, u.username, t.topic_id, t.topic_name, q.quote_id, q.quote_name');
    $this->db->from('users u');
    $this->db->join('topics t', 't.user_id = u.user_id'); // this joins the user table to topics
    $this->db->join('quotes q', 'q.topic_id = t.topic_id'); // this joins the quote table to the topics table
    $query = $this->db->get();
    

    您的结果集将类似于:

    user_id     | username  | topic_id  | topic_name    | quote_id  | quote_name
    1           |Thomas     |1          |Whatever       |1          |One quote, anot...
    2           |Ryan       |4          |Another...     |6          |To be or not to...
    

    一旦你有了那个结果集,只需遍历数据以输出它,并检查你是否有来自同一个人的多个报价(比如按 user_id 排序,如果是同一个人,则在第二个循环上进行测试,否则输出新用户名)。

    【讨论】:

    • 感谢您的参考。 Jakub,你能看看下面的 cmets 吗?在我解决这个问题之前,我今晚无法闭上眼睛 :)
    • 我用一个对你有用的活动查询更新了我的答案。
    • @Jakub,它看起来非常棒,谢谢你帮助我。但是我收到了这个错误:Unknown column 't.quote_id' in 'on clause' 我迷失在这个 SQL 地狱中,但我觉得它就在我的眼前。为了清楚起见,这是我的表格和列:pastebin link你能发现我的失败吗?
    • @nielsiano,对不起,我犯了一个错误,我已经更正了活动查询,它应该是 $this->db->join('quotes q', 'q.topic_id = t.topic_id'); 我之前有 quote_id,因此你的错误
    • 我添加的评论是,最近我一直在 CodeIgniter 中使用连接,但以上面的原始问题为例,我的查询结果包含同一行的多次出现的数据集,当表 2 有多个记录与表 1 中的一行相关联时。例如,表 1 中有一个用户。但是那个用户在表 2 中有两行引号。上面的查询返回同一用户的两行。唯一不同的是引号的值。所以我最终循环了相同的数据,两次。
    【解决方案2】:

    如果您想要特定用户的所有引号:

    $this->db->join('TOPICS t', 'u.user_id on t.user_id')
             ->join('QUOTES q', 't.topic_id on q.topic_id')
             ->where('u.user_id', $userId)
             ->get('USERS u');
    
    // I always echo my queries when developing to make sure they are what i'm expecting
    echo $this->db->last_query();
    

    如果您想要所有用户的所有引号

    $this->db->join('TOPICS t', 'u.user_id on t.user_id')
             ->join('QUOTES q', 't.topic_id on q.topic_id')
             ->get('USERS u');
    
    echo $this->db->last_query();
    

    【讨论】:

    • 尝试了第一个查询,但我最终得到了这个错误:错误号:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'on t.user_id JOIN QUOTES q ON t.topic_id on q.topic_id WHERE u.@987654329 附近使用正确的语法@USERS` u) 加入 TOPICS t ON u.user_id on t.user_id 加入 QUOTES q ON t.topic_id on 6543_id WHERE @98778 @ = 1
    • 你能发布得到回应的查询吗?
    • 我通过 'u.user_id = t.user_id' 而不是 on 来修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多