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.
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 ...
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()
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With