codeigniter ActiveRecord where statement issue

好久不见. 提交于 2020-03-06 09:15:37

问题


is have this statement and i want to make it by the Active Records way in codeigniter

DELETE FROM TABLE 
(col1 = value AND col2 = value2 ) OR (col1 = value2 AND col2 = value );

回答1:


CodeIgniter Active Record is impractical for mixing AND's and OR's within a query.

You will likely need to construct some of the query manually, e.g. something along the lines of:

$this->db->where("col1 = $value AND col2 = $value2");
$this->db->or_where("col1 = $value2 AND col2 = $value");

Alternately, in your example where there are only two specific columns and two values, the following idea should also work:

$this->db->where_in('col1', array($value, $value2));
$this->db->where_in('col2', array($value, $value2));
$this->db->where('col1 != col2');



回答2:


Try this:

$this->db->where('col1', $value1);
$this->db->where('col2', $value2);
$this->db->delete($table);



回答3:


That's not the goal of active record. As name suggest active record object instance is tied to a single row in a table. If you want to do this in "active record way" you have to retrieve all the records you have to delete the database, mark them as deleted and commit changes witch is overkill. Best you can do is to get connection instance from codeignither and execute query manually.



来源:https://stackoverflow.com/questions/7512116/codeigniter-activerecord-where-statement-issue

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