Left join not working in codeigniter - codeigniter

I written query to get record from main table tpl_upload_csv_file and get related record from tbl_process_csv. I get all the records but the problem is if i have 1 id from tpl_upload_csv_file in tbl_process_csv for 5 rows its displaying 5 times. Same record is displaying 5 times.
$this->db->select('tbl_process_csv.id, tbl_process_csv.record_no, tbl_process_csv.reason,tpl_upload_csv_file.uploaded_file_name, tpl_upload_csv_file.uploaded_date_time');
$this->db->from('tpl_upload_csv_file');
$this->db->where('tbl_process_csv.process_status', 3);
$this->db->join('tbl_process_csv', 'tbl_process_csv.csv_file_id = tpl_upload_csv_file.id', 'left');
$this->db->order_by('tbl_process_csv.date_of_processing', 'desc');
$query = $this->db->get();
print_r($query->result());die;
return $query->result();
My table structure is
tpl_upload_csv_file :
id
uploaded_file_name
uploaded_date_time
records_available
tbl_process_csv :
id
csv_file_id -->(Reference if for table tpl_upload_csv_file)
record_no
process_status
reason

Try this and let me know if any problem occur:
$this->db->select('tbl_process_csv.id, tbl_process_csv.record_no, tbl_process_csv.reason,tpl_upload_csv_file.uploaded_file_name, tpl_upload_csv_file.uploaded_date_time');
$this->db->from('tpl_upload_csv_file');
$this->db->join('tbl_process_csv', 'tbl_process_csv.csv_file_id = tpl_upload_csv_file.id', 'left');
$this->db->where('tbl_process_csv.process_status', 3);
$this->db->group_by('tbl_process_csv.csv_file_id');
$this->db->order_by('tbl_process_csv.date_of_processing', 'desc');
$query = $this->db->get();
return $query->result();

$this->db->distinct('tbl_process_csv.id');
Add this line in your code.
So, you will get distinct records w.r.t tbl_process_csv.id

Related

How to retrieve data between two dates in PHP codeingiter?

i am retrieving data between two date range and i am getting records also but when i select start date same as end date it is giving me blank even though i have record of that date in database table, only those records have different time.If anyone know solution please let me know.Below is my query to fetch records between date range.
Thanks in advance.
$this->db->select('*');
$this->db->from('transaction_tbl');
$this->db->where('user_id',$user_id);
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$query = $this->db->get();
you need to specify time also between two dates
$startDate=date("Y m d",strtotime($startDate)).' 00:00'; //00:00 start day time
$endDate=date("Y m d",strtotime($endDate)).' 23:59'; //23:59 end day time
$this->db->select('*');
$this->db->from('transaction_tbl');
$this->db->where('user_id',$user_id);
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$query = $this->db->get();
try this
$this->db->query("SELECT * FROM transaction_tbl WHERE user_id = '$user_id' && date BETWEEN '$startDate' AND '$endDate'");
$this->db->select('*');
$this->db->from('transaction_tbl');
$this->db->where('user_id',$user_id);
$this->db->where('date <=',$startDate);
$this->db->where('date >=',$endDate);
$query = $this->db->get();
Try this in your model.
function your_model($user_id,$startDate,$endDate){
$query=$this->db->query("SELECT * FROM transaction_tbl WHERE user_id =
$user_id && date BETWEEN $startDate AND $endDate");
return $query->result_array();
}

How to select all column from multiple tables in codeigniter using join?

I have 4 tables like this:
Now, I want to select all columns from all tables where model.featured=1. i.e.
model.id
model.name
model_attributes.id
model_attributes.attributes_value
model_images.id
model_images.model_images
attributes.id
attributes.name
attributes.value
I can only do basic level queries and I'm not sure if I'm anywhere near to the solution but this is what I have tried (returns nothing):
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model.id = model_images.model_id','RIGHT');
$this->db->join('model_attributes','model.id = model_attributes.model_id','RIGHT');
$this->db->join('attributes','model_attributes.attributes_id = attributes.id','RIGHT');
$this->db->where('model.featured', 1);
$query = $this->db->get();
return $query->result();
How do I achieve what I want ? Or, is there any other better methods to do it ?
// try this query
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model_images.model_id = model.id');
$this->db->join('model_attributes','model_attributes.model_id = model.id');
$this->db->join('attributes','attributes.id = model_attributes.attributes_id');
$this->db->where('model.featured','1');
$query = $this->db->get()->result();
return $query;
Please go through below mentioned solution.
$this->db->select('model.*,model_images.*,model_attributtes.*, attributes.*'):
Let me know if it not works for you.
Please go through below solution. If multiple table has same column name then you have to give alias for each column name which have same name in both the table. because by default CI merge all column name and generate result.
$this->db->select('*,c.name as c_name,s.name as s_name');
$this->db->from('country c');
$this->db->join('state s', 'c.id = s.country_id');
$query = $this->db->get();

2 join conditions codeigniter giving error

I have 2 tables - for orders and for documents for these orders. I want to show all orders from first table and if there are documents for these orders, to show date of documents(I take date from table documents).
I'm trying to make 2 join conditions because in table documents if I only join on idOrder, there are more than 1 row and I have to join on documents.name to be same as $this->uri->segment(3).
But I'm using the following code and it gives me error. Could I use 2 join conditions in this way:
My model is:
<?php
public function get_all_orders($user_id){
$this->db->select('ordersheader.*, customer.name as customerName,documents.date_make');
$this->db->from('ordersheader');
$this->db->join('customer', 'ordersheader.idCustomer = customer.idCustomer');
$this->db->join('documents', 'ordersheader.idOrder = documents.idOrder','left');
$this->db->join('documents as D', 'D.name="declaration"','left');
$this->db->where('ordersheader.user_id', $user_id);
$query = $this->db->get();
return $query->result_array();
}
Edited: I found solution. But it's written in pure sql, how to write it with Codeingiter syntax? My new working code is:
<?php
$query=("SELECT ordersheader.*, customer.name as customerName,documents.date_make,documents.deactivated_at
FROM ordersheader JOIN customer ON ordersheader.idCustomer = customer.idCustomer
LEFT JOIN documents
ON ordersheader.idOrder = documents.idOrder AND documents.name = '".$document."'
WHERE ordersheader.user_id = '".$user_id."' ");
$result = $this->db->query($query);
return $result->result_array();
This is wrong in your query
$this->db->join('documents as D', 'D.name="declaration"','left');
you can compare column using where clause
public function get_all_orders($user_id){
$this->db->select('ordersheader.*, customer.name as customerName,documents.date_make');
$this->db->from('ordersheader');
$this->db->join('customer', 'ordersheader.idCustomer = customer.idCustomer');
$this->db->join('documents', 'ordersheader.idOrder = documents.idOrder','left');
// $this->db->join('documents as D', 'D.name="declaration"','left'); / comment this line
$this->db->where('documents.name', 'declaration');
$this->db->where('ordersheader.user_id', $user_id);
$query = $this->db->get();
return $query->result_array();
}

codeigniter active record class

this is my normal mysql command
$query = $this->db->query("SELECT `house_details`.`houses_id` , `house_details`.`house_dis` , `house_details`.`house_type` , `house_details`.`area` , `house_details`.`cost` , `house_details`.`user_id` , `image`.`thumb_path`
FROM (
`house_details`
)
LEFT JOIN `image` ON `image`.`house_id` = `house_details`.`houses_id`
AND `house_details`.`status` =1
GROUP BY `houses_id` DESC
LIMIT $start,$limit ");
return $query->result();
what is the similar code with CI active record class ??
Try this,
$this->db->select('house_details.houses_id, house_details.house_dis, house_details.house_type,house_details.area,house_details.cost,house_details.user_id,image.thumb_path');
$this->db->from('house_details');
$this->db->join('image', 'image.house_id = house_details.houses_id','left');
$this->db->where('image.status', 1);
$this->db->order_by("house_details.houses_id DESC");
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result();
Assumed there is no group by since you wanted to order result set in descending order. If group by is there changed it to group_by and remove 'DESC'.
Try this:
$this->db->select('...')->
from('house_details')->
join('image','image.house_id=house_details.houses_id AND image.status=1','LEFT')->
where('...')->
order_by('house_details.houses_id','DESC')->
limit($start,$limit);

Codeigniter: Select from multiple tables

How can I select rows from two or more tables?
I'm setting default fields for a form, and I need values from two tables...
My current code reads:
$this->CI->db->select('*');
$this->CI->db->from('user_profiles');
$this->CI->db->where('user_id' , $id);
$user = $this->CI->db->get();
$user = $user->row_array();
$this->CI->validation->set_default_value($user);
The example in the User Guide should explain this:
$this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
// Produces:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id
See the whole thing under Active Record page in the User Guide.
Just add the other table to the "->from()" method. Something like:
$this->db->select('t1.field, t2.field2')
->from('table1 AS t1, table2 AS t2')
->where('t1.id = t2.table1_id')
->where('t1.user_id', $user_id);
I think the question was not so much about joins as how to display values from two different tables - the User Guide doesn't seem to explain this.
Here's my take:
$this->db->select('u.*, c.company, r.description');
$this->db->from('users u, company c, roles r');
$this->db->where('c.id = u.id_company');
$this->db->where('r.permissions = u.permissions');
$query = $this->db->get();
I think the syntax is incorrect.
You need to select one record. I have two tables, and I have an id from one table transfer by parameter, and the relation of both tables.
Try this
$this->db->select('*')
->from('student')
->where('student.roll_no',$id)
->join('student_details','student_details.roll_no = student.roll_no')
->join('course_details','course_details.roll_no = student.roll_no');
$query = $this->db->get();
return $query->row_array();
$SqlInfo="select a.name, b.data fromtable1 a, table2 b where a.id=b.a_id";
$query = $this->db->query($SqlInfo);
try this way, you can add a third table named as c and add an 'and' command to the sql command.
// Select From Table 1 All Fields, and From Table 2 one Field or more ....
$this->db->select('table1.*, table2.name');
$this->db->from('table1, table2');
$this->db->where('table2.category_id = table1.id');
$this->db->where('table2.lang_id',$id); // your where with variable
$query = $this->db->get();
return $query->result();

Resources