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
Related
code:
public function draft_post($idd)
{
$this->db->select('*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In this codes, I have two table i.e. registration and draft_registration. Now, What am I doing here I want to run inner join in Codeigniter. Now, What happening when I hit this query on phpmyadmin it shows wrong data i.e. I have two rows in draft_registration and one row in registration table but it always shows two table which is wrong and my query looks like when I was print as mention below:
SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'
So, How can I resolve this issue? Please help me.
Thank You
$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.
For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');
Specify column that you want to select. Or if you want select all column of your table, you can use :
SELECT registration.* with backticks `` on column name
Use the Below Code
public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Or you can use with objects
public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
how to write simple query into codeigniter query using join righ?????
$query = $this->db->query("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,
staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id
FROM staff_role_permissions
RIGHT JOIN staff_permissions_list ON staff_role_permissions.permission_id=staff_permissions_list.id
AND staff_role_permissions.role_id=$id WHERE staff_permissions_list.perm_type=0
ORDER BY staff_permissions_list.id ASC
");
if ($query->num_rows() > 0) {
return $query->result_array();
}
$this->db->select('book_id, book_name, author_name, category_name');
$this->db->from('books');
$this->db->join('category', 'category.category_id = books.category_id', 'right');
$query = $this->db->get();
you can get data using this method of right join
How about that ?
$query = $this->db
->select("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id")
->from("staff_role_permissions AS srp")
->join("staff_permissions_list AS spl","srp.permission_id = spl.id","right")
->where("spl.perm_type","0")
->where("srp.role_id",$id)
->order_by("spl.id","ASC")
->get();
i put the role_id to the where section - maybe you need to put it back (not sure what you want to achieve here)
using code igniter! I have two tables student and student_class with foreign key student_id, i want to pick data which exists on student table but not found on class_student
here is my sql
function student_class(){
$this->db->SELECT ('student.student_id, student.firstname, student.middlename, student.lastname');
$this->db->FROM('student');
$this->db->WHERE('student.status',0);
$this->db->JOIN('student_class', 'student_class.student_id=student_class.student_id', 'left');
$this->db->where_not_in('student_class.student_id');
$query =$this->db->get();
return $query->result_array();
}
it does not work out!!
can i get help..
Try like this..
First find all student ids that are matched with student_class table.Then use $this->db->where_not_in to get your required result.
function student_class(){
$this->db->select ('student.student_id');
$this->db->from ('student');
$this->db->join('student_class', 'student.student_id=student_class.student_id', 'left');
$this->db->where('student.status',0);
$data = $this->db->result_array();//array of matched id's
$this->db->select('student_id,firstname, middlename,lastname');
$this->db->from('student');
$this->db->where_not_in($data);
$query = $this->db->get();
return $query->result_array();
}
Hope it will works.
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');
I try this code
function catstore($id)
{
$this->db->distinct();
$this->db->select('store_id');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$result = $this->db->get();
return $result->result();
}
it produces only the distinct store_id but i want all column of the table with distinct rows based on store_id
Suggest me any option
Thanks in advance
You can try using group_by
function catstore($id)
{
$this->db->select('*');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$this->db->group_by('store_id');
$result = $this->db->get();
return $result->result();
}
I know this question is already long time ago, but one may be looking for the same problem like me, and this is how I solved it.
you can omit the select function altogether. If you are selecting all (*) from a table you do not need to use this function. When omitted, CodeIgniter assumes you wish to SELECT *
See http://codeigniter.com/user_guide/database/active_record.html#select
Or..
You can pass comma separated values to select function. So pass all column names separated by comma that you want to select
function catstore($id)
{
$this->db->distinct();
$this->db->select('store_id, column1, column2, cloumn3');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$result = $this->db->get();
return $result->result();
}