我必须查询具有>=
和=<
的where条件的内容,但我没有运气。这是在 CODEIGNITER 。
这是mysql查询中的自然方式:
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
这就是我所拥有的:
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();
也许有人可以帮我解决这个问题。感谢。
答案 0 :(得分:10)
将=<
更改为<=
。
我还在phpmyadmin中测试了你当前的查询,因为我无法相信它不会抛出错误。但我的确如此。所以你的查询不应该在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 ...
答案 1 :(得分:6)
尝试将=<
更改为<=
->where('payment.paymentdate >=', $month_start)
->where('payment.paymentdate <=', $month_end)
并且更好但不是cumpolsury在where条件之前加入表。现在你的查询应该像
->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()
答案 2 :(得分:0)
尝试:
$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)
->from('product_key')
->join('payment', 'payment.keyid=product_key.id', 'LEFT OUTER')
->where('payment.paymentdate >=', '2013-08-01')
->where('payment.paymentdate =<', '2013-08-31')
->where('status', 'purchased')
->group_by('product_key.id')
->order_by('client_name', 'asc')
->get();
答案 3 :(得分:0)
据我所知,你可以像这样写出来
$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');
答案 4 :(得分:0)
$this->db->where("DATE_FORMAT(ph_payment_date, '%d-%m-%Y') BETWEEN '$startdate' AND '$enddate'");
$this->db->where("DATE_FORMAT(ph_payment_date, '%d-%m-%Y') >= '$startdate'");
$this->db->where("DATE_FORMAT(ph_payment_date, '%d-%m-%Y') <= '$enddate'");