PDA

View Full Version : استفاده از active record یا کوئری



pary_daryayi
شنبه 10 خرداد 1393, 11:21 صبح
سلام . من همیشه از active record در CI استفاده میکنم . اما الان تو این کوئری خاص نمیدونم چطور ازش استفاده کنم :


SELECT SUBSTR(newdate, 1, 4) as year FROM (`m_article`)

اینطور استفاده کردم ولی جواب نداد :
model:


public function fetch_tb($tb,$filde,$where=NULL){
$this->db->select($filde);
if(!is_null($where)){
$this->db->where($where);
}
$result = $this->db->get($tb);
return $result;
}


controller:


public function select_section(){
$field="SUBSTRING(newdate, 1, 4) as year";
$data['res']=$this->article_model->fetch_tb('m_article',$field);
$this->_out('admin','article','res',$data);
}


view:



foreach($res as $v){ echo $v->year,'<br>';}

خطایی که میده اینه :


Error Number: 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 'as year FROM (`m_article`) ORDER BY `id` ASC' at line 1
SELECT SUBSTR(newdate, `1`, `4)` as year FROM (`m_article`) ORDER BY `id` ASC
Filename: C:\xampp\htdocs\mysite\m-blog\system\database\DB_driver.php
Line Number: 330

plague
شنبه 10 خرداد 1393, 19:06 عصر
اینو تست کن

$this->db->select(" SUBSTR(newdate, 1, 4) as year " ) ;
$this->db->from('m_article');
$result= $this->db->get();

pary_daryayi
شنبه 10 خرداد 1393, 20:23 عصر
ممنونم / دوباره همون خطا رو میده

plague
شنبه 10 خرداد 1393, 23:38 عصر
اینجوری بنویس



$this->db->select(" SUBSTR(newdate, 1, 4) as year " , FALSE ) ;

pary_daryayi
یک شنبه 11 خرداد 1393, 07:30 صبح
ممنونم .. تا اینجا جواب داد . دو سلکت انجام دادم :


$this->db->select("SUBSTR(newdate, 1, 4) as year ",FALSE) ;$this->db->select("SUBSTR(newdate, 6, 2) as month",FALSE) ;

حالا این رو که اضافه میکنم باز خطا میده :


$this->db->select("SUBSTR(newdate, 1, 4) as year ",FALSE) ;$this->db->select("SUBSTR(newdate, 6, 2) as month",FALSE) ;$this->db->group_by(" SUBSTR(newdate, 1, 7) " ,FALSE);
بدون false هم امتحان کردم :
انگار یک کاراکتر اضافه قبل از order by ایجاد میشه :


SELECT SUBSTR(newdate, 1, 4) as year, SUBSTR(newdate, 6, 2) as month FROM (`m_article`) GROUP BY SUBSTR(newdate, `1`, `7)` ORDER BY `id` ASC


این کوئری رو میخوام ایجاد کنم:


SELECT SUBSTRING(newdate, 1, 4) as `year`,SUBSTRING(newdate, 6, 2) as `month`,count(*) as `count` FROM m_article group by SUBSTRING(newdate, 1, 7)

سرچ کردم اینجا (http://stackoverflow.com/questions/9413641/how-to-group-by-date-with-codeigniter-active-record) هم به همین شکل تقریبا نوشته :

plague
یک شنبه 11 خرداد 1393, 17:10 عصر
خب معلومه که بدون fals نباید بزاری و اررور میده !

من عین این کوئری رو روی تیبل خودم اجرا کردم بدون مشکل سال رو به من داد



$this->db->select("SUBSTR(submit_date, 1, 4) as year ",FALSE) ;
$this->db->select("SUBSTR(submit_date, 6, 2) as month",FALSE) ;
$this->db->group_by(" SUBSTR(submit_date, 1, 7) " ,FALSE);
$res = $this->db->get('users');

ولی در کل این کوئری اشتباهه کلا اگه میخای ماه و سال رو بخونی راه های خیلی ساده تری هست (اگه فیلد تاریخت از نوع timestamp باشه )

مثلا


$this->db->select("YEAR(submit_date) as year ",FALSE) ;
$this->db->select("MONTH(submit_date) as month",FALSE) ;

pary_daryayi
دوشنبه 12 خرداد 1393, 06:48 صبح
ممنونم . منم مثل شما نوشتم ولی یک کاراکتر اضافه تولید میشه و خطا میده :



$this->db->select("SUBSTR(newdate, 1, 4) as year ",FALSE) ;
$this->db->select("SUBSTR(newdate, 6, 2) as month",FALSE) ;
$this->db->group_by(" SUBSTR(newdate, 1, 7) ",FALSE);
$result= $this->db->get('article');



فکر کنم بهتر باشه کوئری بنویسم .