问题
I am in the middle of updating an old site. One of the things I am doing is rewriting the database queries because I have changed the structure of the database to make it more flexible and configurable from the admin.
I previously used the following query (and many others like it), which rely on a temporary table:
$query = $this->db->query("SELECT * FROM (SELECT * FROM Links WHERE Cat LIKE ('$c') AND Type LIKE ('%$x%') LIMIT $s, $l) AS T ORDER BY $sort $o");
Because the queries have got more complicated and now involve a lot of joins I have decided to use active record syntax to make them easier to read. The problem is I cannot find any info on how to make a temporary table to apply a second sort to the data using active record; surely this is possible?
Here is my new query:
$this->db
->select('*')
->from('item_categories')
->where('item_categories.cat_id', $c)
->or_where('item_categories.parent_id', $c)
->or_where('item_categories.gparent_id', $c)
->join('item_categories_rel', 'item_categories_rel.cat_id = item_categories.cat_id', 'right')
->join('item_entries', 'item_entries.item_id = item_categories_rel.item_id','right')
->join('ratings_total', 'ratings_total.item_id = item_entries.item_id')
->order_by("item_name", "asc")
->limit($l, $s);
// up to here I want to store as a temporary table then apply next order
//->order_by($sort, $o); - ideally I want to apply a second order like this
$result = $this->db->get();
I would like to apply 'order by' twice but this doesn't work — active record is not that cleaver (or more likeley would be ambiguous), how can I can I do this this within active record?
Any help is much appreciated. Failing the active record approach can anyone suggest how I might apply a sort to the result object?
回答1:
Try check the generated SQL: print $this->db->_compile_select(). And a plus information: CI doesn't support subqueries.
来源:https://stackoverflow.com/questions/12518069/codeigniter-2-active-record-how-do-i-create-temporary-table-to-apply-second-so