codeigniter活动记录复杂的查询 [英] Codeigniter Active records complex query

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

问题描述

> *的 1。我需要在活动记录写*

> *1. I need to write this in Active records *

我需要参加这3个表,但加入的条件是非常非常有选择性

i need to join these 3 tables , but the condition for join is very very selective

$this->db->select(name);
$this->db->from('table0');
$this->db->join('table1','(table1.id=0 AND table0.feild1 = table1.feild1) OR (table1.id=1 AND table0.feild2 = table1.feild2)') // <--- how to write this is my question

我可以做一个简单的连接,但主要问题是实现在加入我所提到above.Also这是一个非常,非常小的一部分的情况!大查询,所以我真的不能改回了原生SQL查询,如:

i could do a simple join but the main problem is achieving the condition in the join that i have mentioned above.Also this is a small part of a very , very ! big query so i really cant change it back to the native sql query like :

$this->db->query('//entire sql query'); //cant do this, need to write ACTIVE RECORDS

在我写的活动记录,萤火引发我一个错误说:

when i write the active records , firebug throws me an error saying :

您必须在您的SQL语法错误;检查手册中   对应于你的MySQL服务器版本的权利语法使用   临近)或者

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') OR

有什么建议?

推荐答案

说实话,我不知道这是可能的。 CI不使用真实的活动记录,因此每一种方法都有非常严格的参数,我没有看到任何可以被欺骗执行您需要的任务。

To be honest, I don't know that it is possible. CI doesn't use true Active Records, so each method has very rigid parameters, and I don't see any that can be tricked into performing the task you need.

我会尝试重新写你的查询了一下:

What I would try is re-writing your query a bit:

$this->db->select(name);
$this->db->from('table0');
$this->db->join('table1','(table1.id=0 AND table0.feild1 = table1.feild1)', LEFT);
$this->db->join('table1','(table1.id=1 AND table0.feild2 = table1.feild2)', LEFT);
$this->db->where('(table1.id=0 AND table0.feild1 = table1.feild1)');
$this->db->or_where('(table1.id=1 AND table0.feild2 = table1.feild2)');

我相信这个查询将返回正确的价值观,但你一定要测试它彻底。希望这至少指向你在正确的方向。

I believe this query would return the correct values, but you'll certainly want to thoroughly test it. Hope this at least points you in the right direction.

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

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