Welcome Guest, Not a member yet? Register   Sign In
SQL/Active Record Question
#1

[eluser]eric cumbee[/eluser]
I am trying to recreate a query in active record, everything else works except implementing the NOT IN portion of it.
Code:
SELECT `voter`.`ID`, `voter`.`Last_Name`, `voter`.`First_Name`,
       `voter`.`Middle_Name`, `voter`.`Age`, `voter`.`Sex`,
       `voter`.`Party`, `voter`.`Demo`, `voter`.`PV`,
       `household`.`Address`, `household`.`City`, `household`.`Zip`
FROM (`voter`)
JOIN `household` ON `voter`.`House_ID`=`household`.`id`
AND `voter`.`id` = '5327673'
AND `voter`.`id` NOT IN (SELECT `voter_id` FROM (`elimination`))
ORDER BY `Last_Name` ASC
LIMIT 30;

And this is my implementation in active record
Code:
function Get_Voters()
        {
            $this->db->select('voter.ID,voter.Last_Name,voter.First_Name,voter.Middle_Name,voter.Age,voter.Sex,voter.Party,voter.Demo,voter.PV,household.Address,household.City,household.Zip');
            $this->db->from('voter');
            $this->db->join('household','voter.House_ID=household.id');
            
            if($this->Last_Name)
            {
                $this->db->like("Last_Name", $this->Last_Name);
            }
            if($this->First_Name)
            {
                $this->db->like("First_Name", $this->First_Name);
            }
            if($this->Middle_Name)
            {
                $this->db->like("Middle_Name", $this->Middle_Name);
            }
//            if($this->Sex)
//            {
//                $this->db->where("Sex",$this->Sex);
//            }
//            if(isset($this->Demo))
//            {
//                $this->db->where("Demo",$this->Demo);
//            }
            
            $this->db->where("CT",$this->CT);
            $this->db->where("Precnum",$this->Precnum);
            $this->db->where_not_in('voter.id',"SELECT `voter_id` FROM (`elimination`)");
            $this->db->orderby("Last_Name","First_Name");
            $this->db->limit(30);
            $query = $this->db->get();
            return $query->result_array();

what am i doing wrong? i am not completely sure if it is my inexperience with active record or if i am asking to much out of active record and need to implement this as regular sql.
#2

[eluser]umefarooq[/eluser]
try to echo your query of active record its properly like this or not by using

Code:
echo $this->db->last_query();

its will work for you
#3

[eluser]Jameson.[/eluser]
Code:
$this->db->where_not_in('voter.id',"SELECT `voter_id` FROM (`elimination`)");

is simply to be replaced with

Code:
$this->db->where('voter.id NOT IN (SELECT `voter_id` FROM (`elimination`))', null, false);




Theme © iAndrew 2016 - Forum software by © MyBB