I want to used group by so that I can get data as country-state-district. I want only display the country then state and a district level want to add checkbox.
$this->db->distinct();
$this->db->select('country_name,s_name,dist_name');
$this->db->from('resource_details');
$this->db->join('location','reso_dtail_location=loc_id');
$this->db->join('go_state', 'go_stste_id = loc_state', 'left');
$this->db->join('go_country', 'num = loc_country', 'left');
$this->db->join('go_dist', 'id = loc_district', 'left');
$this->db->where('loc_id !=1 AND loc_id !=2');
$query = $this->db->get();
//result
$location = $query->result();
but the query does not give right answer after using group_by it only shows 1st record
You can use group by country. So that you can get distinct country name. But states and city you can use group concat.
Related
$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
I have table call jobs and i need to get jobs.user_iduser and jobs.adminId from same table call user
I try this code it output only one user data only.
$this->db->from('jobs');
$this->db->join('user', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('user', 'user.iduser = jobs.adminId', 'left');
then i try like this it output only one user data only.
$this->db->from('jobs');
$this->db->join('user', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('user as admin', 'admin.iduser = jobs.adminId', 'left');
Is there any way to achieve this?
You can try this.
$this->db->select('u.iduser as uiduser, ua.iduser as uaiduser and so on');
$this->db->from('jobs');
$this->db->join('user u', 'u.iduser = jobs.user_iduser', 'left');
$this->db->join ('user ua', 'ua.iduser = jobs.adminId', 'left');
In your case since the user joined in the first join is being replaced by the second join. So you need to try aliasing the tables.
Try this:
$this->db->from('user');
$this->db->join('jobs', 'user.iduser = jobs.user_iduser', 'left');
$this->db->join('jobs', 'user.iduser = jobs.adminId', 'left');
I don't exactly know the data in tables but left join in SQL means:
Select all records from Table A (user in above example),
along with records from Table B (jobs in above example) for
which the join condition is met (if at all). Ref
So, since you have said only one user data is being returned, with jobs being the left table I am assuming the table jobs contains only one user data. Let me know if you don't follow me.
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();
}
Hello i want to join three tables but it fetch all data from user table, i just want firstname and lastname parameter in that
Here is my join query. i want something like join(user.firstname).
$this->db->select('*');
$this->db->from('post');
$this->db->join('user', 'user.id = post.cop_id');
$this->db->join('source', 'post.source_id = source.id');
$query = $this->db->get();
return $query->result();
You can do something like this:
$this->db->select('post.*, source.*, user.firstname, user.lastname');
$this->db->from('post');
$this->db->join('user', 'user.id = post.cop_id');
$this->db->join('source', 'post.source_id = source.id');
$query = $this->db->get();
return $query->result();
The table.* indicates that you want all the fields from that table.
change $this->db->select('*');
to $this->db->select('user.firstname, user.lastname');
You can define that in the select:
$this->db->select(['post.id', 'post.title', 'post.description', 'user.firstname']);
Take a look at this answer https://stackoverflow.com/a/15402766/1897484
I am creating a sort of card collection programme with codeigniter and ion_auth for authentication. A user should be able to add a card to his collection.
So i have a users table and a cards table and a junction/bridge table.
users
ID
NAME
and
cards
ID
NAME
and
users_cards
ID
USER_ID
CARD_ID
I want to accomplish a SELECT statement that retrieves the cards from a users through user_cards.
$this->db->select('*');
$this->db->from('cards');
$this->db->join('users_cards', 'users.id = users_cards.user_id', 'inner');
$this->db->join('users', 'users_cards.user_id = users.id', 'inner');
$query = $this->db->get();
return $query->result_array();
I cant seem to get a grasp at the concept of these joins. Can someone please help me?
Looks like the join statement is missing the reference to the 'cards' table.
$this->db->select('*');
$this->db->from('users_cards');
$this->db->join('users', 'users.id = users_cards.user_id', 'inner');
$this->db->join('cards', 'cards.id = users_cards.card_id', 'inner');
$query = $this->db->get();
return $query->result_array();
The idea is that you're trying to get a relationship going between users_cards and the other two tables by referencing their relevant ids
Since you didn't joined users table you can't join based on the users.id reference. So you need to change
$this->db->join('users_cards', 'users.id = users_cards.user_id', 'inner');
to
$this->db->join('users_cards', 'cards.id = users_cards.card_id', 'inner');