Order by not working with Group by in codeigniter - php

$loginuserID = $this->session->userdata("loginuserID");
$this->db->select('notice_x_user.*,notice.*,classes.*,sub_courses.*,,notice.status as studentStatus,notice.date as noticeDate');
$this->db->from('notice_x_user');
$this->db->join('notice','notice_x_user.noticeID = notice.noticeID', 'LEFT');
$this->db->join('classes', 'classes.ClassesID = notice.classesID', 'LEFT');
$this->db->join('sub_courses', 'sub_courses.sub_coursesID = notice.sub_coursesID', 'LEFT');
$wheres = "(notice_x_user.userID = '".$loginuserID."' and notice_x_user.usertype = 'Support') or notice.userID = '".$loginuserID."'";
$this->db->where($wheres);
$this->db->order_by('notice.noticeID', 'DESC');
$this->db->group_by('notice.noticeID');
I am join four table where I get correct data but the problem is that order by is not working while using group by. So, How can I do this? Please help me.
Thank You

use select_max for getting the max noticeID
$this->db->select('notice_x_user.*,notice.*,classes.*,sub_courses.*,,notice.status as studentStatus,notice.date as noticeDate');
$this->db->select_max('notice.noticeID' , 'noticeID');
$this->db->from('notice_x_user');
$this->db->join('notice','notice_x_user.noticeID = notice.noticeID', 'LEFT');
$this->db->join('classes', 'classes.ClassesID = notice.classesID', 'LEFT');
$this->db->join('sub_courses', 'sub_courses.sub_coursesID = notice.sub_coursesID', 'LEFT');
$wheres = "(notice_x_user.userID = '".$loginuserID."' and notice_x_user.usertype = 'Support') or notice.userID = '".$loginuserID."'";
$this->db->where($wheres);
$this->db->order_by('noticeID', 'DESC');
$this->db->group_by('notice.noticeID');

Grouping statements must be used before ordering statements, otherwise you'll get a MySQL error.
Just put the statements in the correct order:
$this->db->group_by('notice.noticeID');
$this->db->order_by('notice.noticeID', 'DESC');
and it'll work as expected

Related

subquery treated as char in subquery codeigniter

I tried to use subquery in codeigniter to get the result and use the result as the parameter in where in clause. but i got this error.
and here is my codeigniter active record query:
$session['hasil'] = $this->session->userdata('logged_in');
$regional = $session['hasil']->regional;
$this->db->select('a.id_agency');
$this->db->from('tbl_agency a');
$this->db->join('tbl_collector_agency b', 'a.id_agency = b.id_agency', 'left');
$this->db->join('tbl_area_collector c', 'b.id_collector = c.id_collector', 'left');
$this->db->join('mysystem.lapkeu.dbo.groupbranch d', 'c.group_branch_id = d.GroupBranchID', 'left');
$this->db->where('d.regional', $regional);
$subQuery = $this->db->get_compiled_select();
$this->db->reset_query();
$this->db->select('*');
$this->db->from($this->table);
$this->db->where_in('id_agency', $subQuery);
im using microsoft sql server as my database.

Multiple Join query in codeigniter?

function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id');
$this->db->join('student','student_id = fee_collection_roll_id');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}
this would generate NULL output why?
When you are using join query, Please use alias like given below.
$this->db->from('fee_collections AS ac');
$this->db->join('class AS cs','cs.class_id = ac.fee_collection_class_id');
Please try this, This will help you to get output.
Thanks for your edit!
This edit will be visible only to you until it is peer reviewed.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id', 'left');
$this->db->join('student','student_id = fee_collection_roll_id', 'left');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id', 'left');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}
#Ganesh : Please check my below mentioned query, As per db schema shared by you it's in my local system.
$this->db->select('c.name as cname, s.name as sname,fc.*');
$this->db->from('fee_collections fc');
$this->db->join('class c', 'c.class_id = fc.fee_collection_class_id', 'left');
$this->db->join('student s', 's.student_id = fc.fee_collection_roll_id', 'left');
$this->db->join('fee_particulars fp', 'fp.fee_particular_id = fc.fee_particular_id', 'left');
$this->db->join('admin a', 'a.admin_id = fc.fee_collection_added_by');
$this->db->where('fc.fee_collection_receipt', $receipt_id);
$query = $this->db->get();
return $query->row();
Please change select() line as per your table field name.
Let me know if you have any error.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->where('fee_collection_id',$receipt_id);
$this->db->join('class','class_id = fee_collection_class_id');
$this->db->join('student','student_id = fee_collection_roll_id');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id');
$this->db->join('admin','admin_id = fee_collection_added_by');
$query = $this->db->get();
return $query->row();
}
You are not getting any sql error means your query is right but you are not getting data means your condition is wrong.
Without see the table structure its tough to say exact reason. By seeing your code look like right but you can check same sql query by writing raw sql query. It seems some table not meet joinned condition.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id', 'left');
$this->db->join('student','student_id = fee_collection_roll_id', 'left');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id', 'left');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}

MySQL join two table query in codeigniter

I have two table, limit and transaction
$wherehis= array('status'=>'Yes','company_id' =>$compnayid);
$this->db->select('type,tra.amount,tra.qty,limit_id');
$this->db->where($wherehis);
$this->db->limit(100);
$this->db->order_by('tra.tra_id','DESC');
$this->db->join('limit_history as sell', 'tra.sell_limit_id = sell.limit_id','left');
$this->db->join('limit_history as buy', 'tra.buy_limit_id = buy.limit_id','left');
$data['Histroy']= $this->db->get("tbltrancation as tra")->result_array();
but get only sell order....please help me thanx
get the buy and sell both data
Try this,
$this->db->limit(100);
$this->db->order_by('tra.tra_id','DESC');
$this->db->join('limit_history as sell', '(tra.sell_limit_id = sell.limit_id AND tra.buy_limit_id = buy.limit_id)','left');
$data['Histroy']= $this->db->get("tbltrancation as tra")->result_array();
When there are identical column names you need to give them an alias to differentiate them:
$this->db
->select('tra.*, sell.amount as sell_amount, sell.qty as sell_qty, buy.amount as buy_amount, buy.qty as buy_qty')
->from('tbltrancation as tra')
->join('limit_history as sell', 'tra.sell_limit_id = sell.limit_id', 'left')
->join('limit_history as buy', 'tra.buy_limit_id = buy.limit_id', 'left')
->order_by('tra.tra_id', 'desc')
->limit(100);
$data['Histroy'] = $this->db->get()->result_array();

How to write this query in codeigniter active records

How to write this query in codeigniter active records
select
a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,
c.sub_child_cat_id,c.sub_child_cat_name
FROM parent_categories a,child_categories b,sub_child_categories c
WHERE a.parent_cat_id=b.parent_cat_id AND b.child_cat_id=c.child_cat_id
Tried this but it Shows 0 result
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->where('a.parent_cat_id','b.parent_cat_id');
$this->db->where('b.child_cat_id','c.child_cat_id');
$result = $this->db->get()->result_array();
when i echo the above ci query i get
SELECT `a`.`parent_cat_id`, `a`.`parent_cat_name`, `b`.`child_cat_id`, `b`.`child_cat_name`, `c`.`sub_child_cat_id`, `c`.`sub_child_cat_name`
FROM `parent_categories` `a`, `child_categories` `b`, `sub_child_categories` `c`
WHERE `a`.`parent_cat_id` = 'b.parent_cat_id'
AND `b`.`child_cat_id` = 'c.child_cat_id'
Try changing $this->db->where in your query as below-
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->where("a.parent_cat_id = b.parent_cat_id");
$this->db->where("b.child_cat_id = c.child_cat_id");
$result = $this->db->get()->result_array();
You have to use Join Query For That Here is Code Snippet
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a');
$this->db->join('child_categories b', 'b.parent_cat_id = a.parent_cat_id', 'left');
$this->db->join('sub_child_categories c', 'c.child_cat_id = b.child_cat_id', 'left');
$query = $this->db->get();
$res = $query->result();
I didn't have your table structure and data to check. But, try this. It will work.
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->join('a.parent_cat_id','b.parent_cat_id');
$this->db->join('b.child_cat_id','c.child_cat_id');
$this->db->get();

Join query with multiple ON condition not working in codeigniter?

I need a query like this
SELECT * FROM (`users`)
LEFT JOIN `users_phone_numbers`
ON `users`.`id`= `users_phone_numbers`.`user_id`
LEFT JOIN `phone_number`
ON (`phone_number`.`id`= `users_phone_numbers`.`phone_num_id` AND users_phone_numbers.is_active = 1)
WHERE `users`.`id` = 56
i have code like this in codeigniter
$this->db->select('*');
$this->db->from('users');
$this->db->join('users_phone_numbers',
'users.id= users_phone_numbers.user_id',
'left');
$this->db->join('phone_number',
'(phone_number.id= users_phone_numbers.phone_num_id AND users_phone_numbers.is_active = 1)',
'left');
$this->db->where('users.id = '. $id);
$result = $q->result_array();
But i got this error
If you check Codeigniter's handling of the condition function, you will see the following:
// Strip apart the condition and protect the identifiers
if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Codeigniter is stripping the opening bracket from the query. Can you not move the LEFT JOIN additional clause within the WHERE clause to get around this?
EDIT:
$this->db->select('*');
$this->db->from('users');
$this->db->join('users_phone_numbers',
'users.id= users_phone_numbers.user_id',
'left');
$this->db->join('phone_number',
'phone_number.id= users_phone_numbers.phone_num_id)',
'left');
$this->db->where(array('users.id => '. $id, 'users_phone_numbers.is_active' => 1));
$result = $q->result_array();
You need to remove the second condition of the join.
$this->db->join('phone_number',
'phone_number.id= users_phone_numbers.phone_num_id AND users_phone_numbers.is_active = 1',
'left');
remove brackets like above. it works for me when there is no bracket

Categories