17

我必须查询具有 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(); 

也许有人可以帮助我。谢谢。

4

5 回答 5

15

更改=<<=

我还在 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 ...
于 2013-08-30T08:43:34.543 回答
11

尝试更改=<<=喜欢

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

在where条件之前加入表更好但不是cumpolsury 。现在你的查询应该像

->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()
于 2013-08-30T08:47:09.430 回答
1

据我所知,你可以这样写

$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');
于 2017-08-11T04:33:01.060 回答
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();
于 2013-08-30T08:44:18.603 回答
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'");
于 2019-06-13T08:16:57.117 回答