php – 使用Active Record在CodeIgniter中加入SQL
我试图绕过这个,但我似乎是围成一圈.我正在尝试逐个列出用户主题,下面是属于该特定主题的引号.如果这是有道理的. 我有3个表,如下所示:
我希望能够在我看来做这样的事情: 马上我会问“什么不工作?”,其次我建议您运行探查器向您展示正在生成的EXACT SQL,这样您就可以对ACTIVE QUERY失败的位置进行有效评估. 要使用分析器,请将其粘贴到控制器中: $this->output->enable_profiler(TRUE); 它将导致所有数据库调用,所有POST变量等的良好输出; UPDATE 因此,要完全执行您想要的操作,您需要一个返回以下列的查询: 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排序,如果它是同一个人,则在第二个循环上进行测试,否则输出新用户名). (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |