2013-08-30 187 views
8

我必須查詢具有「> =」和「= <」的where條件的東西,但我沒有運氣。這是在CODEIGNITER中。'大於或等於'和'小於或等於'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

你嘗試使用之間? –

+0

或嘗試明確地將硬編碼日期轉換爲日期 –

+0

@ ashutosh-arya我以前從未在我的任何查詢之間使用過。特別是現在我使用codeigniter作爲我的框架。但我現在要搜索如何使用它。謝謝你的建議。 –

回答

7

更改=<<=

我也在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 ... 
+0

是的,我剛纔發現。對我粗心大意抱歉。感謝大家的幫助! :) –

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(); 
+0

盡我多少想這樣做,但我不能。我的變量因當前日期而異。 –

5

嘗試將=<改變<=

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

而且更好但不cumpolsury的地方condition.Now查詢之前參加表應該像

->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() 
+0

原始SQL不包含任何'OR': - ? –

+0

@Gautam:回滾,然後再添加相同的?爲什麼? –

+0

其實我想建議他使用or_where,但它可能對他沒用......我編輯過thax。 – Gautam3164

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');