I am trying to get data from others table but when I join serviceplan table the result of query is empty but when I remove serviceplan table its fetched data perfectly.
Here is the code of my Model(without serviceplan table-working)
public function send_mail() {
// $user_id=$this->session->userdata('user_id');
$query=$this->db->select('*, employee.name_emp as emp_name, customer.name as cust_name, servicetype.name_set as s_name',
'serviceplan.price as p_rice')->from('appointment')
// ->where('id_app',$appointment_id)
->join('customer', 'customer.id= appointment.name_app')
->join('servicetype', 'servicetype.id_set= appointment.sertype')
->join('employee', 'employee.id_emp= appointment.emp')
// ->join('serviceplan', 'serviceplan.id_sep= appointment.price_app')
->get();
echo $this->db->last_query();
exit();
return $query->result();
}
Normal joins or Inner Join only gets the rows common in both the tables. So, in your case, there are not matching conditions in your serviceplan table.
Verify it. Or look upon your requirements and try using leftjoin instead of join.
Also look: Difference between joins
You may try this simple join table query using codeigniter as below:
$email='myEmail#test.com';
$this->db->select('*');
$this->db->from('table1');
$this->db->where('table1.email',$email);
$this->db->join('table2', 'table1.email = table2.email');
$query = $this->db->get();
$assignedData=$query->result_array();
Related
This is my code in model
function load_roster_by_date()
{
$date = $this->input->post('date');
$this->db->select('*');
$this->db->from('duty_assign');
$this->db->where('date',$date);
$this->db->join('opd_employees','(opd_employees.badgenumber = duty_assign.emp_id)','right');
$query = $this->db->get();
return $query->result_array();
}
The code perfectly works without adding $this->db->where('date',$date);. (Which means the output is the expected right joined table without a where condition). But when I add the where condition, the result is similar to the regular join, not as the right join. No errors in the console. Any help to solve this, please?
All I want is whether a duty added or not all the employee names
should be displayed in the table
$this->db->where('date',$date);
can you try to:
$this->db->where('opd_employees.date',$date);
or
$this->db->where('duty_assign.date',$date);
cause this db already select join
I think this will help you understand the difference between Joins.
There must be no data in your database.
$this->db->get_compiled_select(); => It will help you see the query made be CI which you can execute directly on your server to see the results.
You can see this answer What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? to understand "Joins".
Please add where condition below the join.
function load_roster_by_date()
{
$date = $this->input->post('date');
$this->db->select('*');
$this->db->from('duty_assign');
$this->db->join('opd_employees','(opd_employees.badgenumber = duty_assign.emp_id)','right');
$this->db->where('date',$date);
$query = $this->db->get();
return $query->result_array();
}
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;
}
I'm trying to join 2 tables with the same id and same username to check if the row exists. With the query, I use I always get the Undefined variable: query from my view.
I'm using this query:
$query= DB::table('table1')->select('table1.id')
->join('table2','table2.id','=','table1.id')
->where('active','=','1', 'username','=',Auth::user()->name)->get();
return view('table1', ['table1' => $query]);
Please note that without the joint query it works fine. Same variable name and I can get the ID from table1.
Now I'm not really good at joining 2 tables so I might have an error somewhere in there or my return view is wrong. Without the joint query, I use the same return view as with joins. Maybe that's the problem.
EDIT: I was passing old variable from my old query along. Forgot to close it. Now it passes the variable. Only need to figure the right join query now.
You can easy to join two table using leftJoin. Try this code.
$query = DB::table('table1')
->leftJoin('table2', 'table1.id', '=', 'table2.id')
->where('active','=','1', 'username','=',Auth::user()->name)->get();
return view('table1', ['table1' => $query]);
try using like this.
$query= DB::table('table1')->select('table1.id')
->join('table2','table2.id','=','table1.id')
->where(['table1.active'=>'1', 'table1.username'=>Auth::user()->name])->get();
return view('table1', ['table1' => $query]);
The join query u used will only show value/data only both the table has data in it to display, at the same time if u use left join it will display even if there is no data available in the next table or the second table you are trying to access.
$query= DB::table('table1')
->leftJoin('table2','table2.id','=','table1.id')
->where('active','=','1')
->where('username','=',Auth::user()->name)
->select('table1.id')
->get();
if($query) // this means there is data in $query
{
return view('table1', ['table1' => $query]);
} else { // there is no data in $query
return view('table1', ['table1' => "no data available"]);
}
I have the following query:
$this->db
->select('SQL_CALC_FOUND_ROWS null as rows
,table1.*
,table2.*
,table3.*', FALSE)
->from('table1')
->where('table1.column1', $user_id)
->join('table2', 'table2.column2 = table1.column2')
->join('table3', 'table3.column2 = table1.column2')
->group_by('table1.column2')
->order_by('table1.column2', 'DESC');
$query = $this->db->get();
The problem is, there may not be a row in table 3 and if there is not, I would still like to return a result with the remainder of the query data. Please could someone advise how to achieve this?
you should do a left join on table3
Use left join and also use group_by to get exact records:
$this->db->join('login_ranknames AS t4', 't4.id = t1.rank', 'left');
$this->db->group_by('t4.id');
hi everyone i am new to codeigniter and currently working on a small project in the project i am trying to join two tables and display there data in single table. i looked at the user guide that codeigniter has an i am not sure how this work
$this->db->join();
what table should be first and what id key should be firs. Can someone explain me more in detail about this please use examples if u can. I am trying to join credential table and tblanswers. Tnx for answering.
i have tried to code a function using this example:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
EDIT:
instead of using join method in codeigniter is it possible to use a simple function to retrieve the two table data separately? all i want is to echo the data from database table on to a html table in my website page to be displayed is it possible to write two get functions to retrieve two tables separately ?
It doesn't matter what table is first... Simply:
<?php
$this->db->select('t1.name, t2.something, t3.another')
->from('table1 as t1')
->where('t1.id', $id)
->join('table2 as t2', 't1.id = t2.id', 'LEFT')
->join('table3 as t3', 't1.id = t3.id', 'LEFT')
->get();
?>
here is how it works:
suppose we have two tables namely student, library.
note: but remember that one of the column should match if you want to use where condition/ here we assume that both tables have std_id column
Write the the select query as follows, in the brackets write what are all the things you want
note:write as shown below don't put quotes to each single one put it on whole at once.
*note: suppose we want name, phone_no. from student table and book_name form library table.*
$this->db->select('name, phone_number, book_name');
Now write the from query and write one of the table name(No rule)
$this->db->from('student');
Now join this with the another table with join query
$this->db->join('library', 'students.std_id=library_std_id');
Now write the where condition that like you want book name form library table where std id=1(in practical you need to fetch this id from view/database)
$this->db->where('std_id', 1);
$q= $this->db->get();
That's it it's done now you can print and check the result.
$this->db->join('comments', 'comments.id = blogs.id');
With this line you tell: search me inside comments all comments with id equal blogs.id.
Usually is something like that I think:
$this->db->join('comments', 'comments.blogs_id = blogs.id');
You have to insert into your table a field named blogs_id (int value unisgned) because blogs can have more comments.
Isn't important the position of first or second value
Hi this will work for joining two tables in codeIgnator.
$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
$this->db->from('chat');
$this->db->join('post', 'chat.id = post.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}
You can change to the query as you like do trail and error method to get your appropriate results.
This is my code for joint many tables as possible.
I have 3 tables professeurs,publications and support.
public function toutesdonnées(){
$this->db->select("*");
$this->db->from('publication');
$this->db->join('support', 'publication.idsup = support.idsup');
$this->db->join('professeurs', 'publication.emailprof = professeurs.emailprof');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}