Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Greater than or equal' and 'less than or equal' CODEIGNITER

I have to query something that has a where condition with >= and =< but I have no luck. This is in CODEIGNITER.

This is the natural way in mysql query:

SELECT COUNT(payment.keyid) AS rec_count, `product_key`.`client_name`, 
`product_key`.`contact_email`, `product_key`.`status`, `product_key`.`id`, 
`payment`.`paymentdate`, (payment.id) as pid, `payment`.`subscription_type` 
FROM (`product_key`) 
LEFT OUTER JOIN `payment` ON `payment`.`keyid`=`product_key`.`id` 
WHERE `payment`.`paymentdate` >= '2013-08-01' 
    AND `payment`.`paymentdate` =< '2013-08-31' 
    AND `status` = 'purchased' 
GROUP BY `product_key`.`id` 
ORDER BY `client_name` asc

And this is what I have:

    return $this->db
    ->select('COUNT(payment.keyid) AS rec_count')
    ->select('product_key.client_name, product_key.contact_email, product_key.status, product_key.id, payment.paymentdate, (payment.id) as pid,payment.subscription_type')
    ->from('product_key')          
    ->where('payment.paymentdate >=', $month_start)
    ->where('payment.paymentdate =<', $month_end)
    ->where('status', 'purchased')
    ->join('payment', 'payment.keyid=product_key.id', 'left outer')
    ->order_by('client_name', "asc")
    ->group_by('product_key.id')
    ->get()
    ->result(); 

Maybe someone could help me on this. Thanks.

like image 965
elimariaaa Avatar asked Aug 30 '13 08:08

elimariaaa


3 Answers

Change =< to <=.

I also tested your current query in phpmyadmin, because i could not believe that it does not throw an error. But mine does it. So your query should not work in phpmyadmin.

#1064 - 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 '=< ...' at line ...
like image 115
Tobias Golbs Avatar answered Nov 13 '22 17:11

Tobias Golbs


Try to change the =< to <= like

->where('payment.paymentdate >=', $month_start)
->where('payment.paymentdate <=', $month_end)

And better but not cumpolsury to join the table before the where condition.Now your query should be like

->select('COUNT(payment.keyid) AS rec_count')
->select('product_key.client_name, product_key.contact_email, product_key.status, product_key.id, payment.paymentdate, (payment.id) as pid,payment.subscription_type')
->from('product_key')         
->join('payment', 'payment.keyid=product_key.id', 'left outer')    
->where('payment.paymentdate >=', $month_start)
->where('payment.paymentdate <=', $month_end)
->where('status', 'purchased')
->order_by('client_name', "asc")
->group_by('product_key.id')
->get()
like image 38
Gautam3164 Avatar answered Nov 13 '22 17:11

Gautam3164


from what i know, you can write them like this

$this->db->select('COUNT(payment.keyid) AS rec_count, product_key.client_name, product_key.contact_email, product_key.status, product_key.id, payment.paymentdate, (payment.id) as pid, payment.subscription_type', false);
$this->db->where('payment.paymentdate >= "2013-08-01"');
$this->db->where('payment.paymentdate <= "2013-08-31"');
$this->db->where('status', 'purchased');
$this->db->group_by('product_key.id');
$this->db->order_by('client_name', 'asc');
$this->db->join('payment', 'payment.keyid=product_key.id', 'LEFT OUTER')
$this->db->get('product_key');
like image 1
Indah Avatar answered Nov 13 '22 19:11

Indah