Codeigniter Active records complex query

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

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

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 :

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

any suggestions ?


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.

链接地址: http://www.djcxy.com/p/43776.html

上一篇: SELECT字符串AS列

下一篇: Codeigniter Active记录复杂的查询