Welcome Guest, Not a member yet? Register   Sign In
Issue with Query
#1

Angry

So I have been trying to figure this out and have hit a wall.

This is the query that the profiler prints out:
Code:
SELECT *
FROM (`employees`)
WHERE `position` = 1
OR `position` = 3
AND `status` = 1
ORDER BY `position` desc  

However, the AND status = 1 gets ignored and I don't understand why.
Reply
#2

Code:
SELECT *
FROM (`employees`)
WHERE (`position` = 1
OR `position` = 3)
AND `status` = 1
ORDER BY `position` desc  
Reply
#3

(11-23-2014, 09:50 PM)ivantcholakov Wrote:
Code:
SELECT *
FROM (`employees`)
WHERE (`position` = 1
OR `position` = 3)
AND `status` = 1
ORDER BY `position` desc  

I can't seem to recreate that using Active Record.

My current code is
Code:
$this->db->where("position = 1");
$this->db->or_where("position = 3");
Reply
#4

I think that in this case you can use IN instead of OR AND..>
PHP Code:
$this->db->where_in'position', array(1,3)); 

If its not option to use IN just write your where cause inline:
PHP Code:
$this->db->where('(position=1 OR position = 3) AND status = 1'); 
Best VPS Hosting : Digital Ocean
Reply
#5

This is possible in CodeIgniter 3:

Code:
$this->db
    ->select()  // May be removed, if you select all the fields.
    ->from('employees')
    ->group_start()
        ->where('position', 1)
        ->or_where('position', 3)
    ->group_end()
    ->where('status', 1)
    ->order_by('position', 'desc');

Result SQL:
Code:
SELECT *
FROM `employees`
WHERE   (
`position` = 1
OR `position` = 3
)
AND `status` = 1
ORDER BY `position` DESC
Reply
#6

This solved it:
(11-27-2014, 01:25 PM)sv3tli0 Wrote: I think that in this case you can use IN instead of OR AND..>

PHP Code:
$this->db->where_in'position', array(1,3)); 

Thanks to everyone for helping out!!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB