Codeigniter Active records complex query

帅比萌擦擦* 提交于 2019-12-08 12:32:49

问题


> *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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/12214080/codeigniter-active-records-complex-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!