I need to join and fetch all data from 3 tables and need to group it using a particular tables field name.Am using codeigniter and sql server
my query:
$this->db->select('table1.*,table2.*,table3.*');
$this->db->from('table1');
$this->db->join('table2','table1.id = table2.id');
$this->db->join('table3','table1.sid = table3.sid');
$this->db->group_by('table1.field_name');
Is it possible?
Am getting this error:
fieldname is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Generally, when you do a Group By in SQL Server, the columns you are permitted to include in your Select statement are limited to the columns in your Group By clause, plus a constant or an aggregate function. So you could do this:
Select Table1.field_name, Count(*) as ColumnCount
From Table1
Group By Table1.field_name
But you cannot include any other columns from Table1 unless they are in the Group BY.
use this in model
public function anyfunction($table3_id)
{
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table2.table2_id=table2.id', 'left');
$this->db->join('table3', 'table3.table1_id=table1.table1_id', 'left');
$this->db->where('table3.table1_id',$table3id);
$this->db->order_by('table3.field_name','asc');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result_array();
}
else
{
return false;
}
}
i hope it will work for you...
Related
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();
I'm working on a project using codeigniter 3 and I have a following query
$this->db->select("*");
$this->db->from("questions");
$this->db->join('users', 'users.id = questions.user_id');
$this->db->where('article_id', $articleId);
$this->db->order_by('questions.id', 'DESC');
$query = $this->db->get();
return $query->result();
// query
// SELECT * FROM `questions` JOIN `users` ON `users`.`id` = `questions`.`user_id` WHERE `article_id` = 18 ORDER BY `questions`.`id` DESC
Currently, the id property from users table overrides the questions id one. The question's id is crucial.
I worry, I will have to write a custom query, which I am not really good at.
Try to do this as follow:
$this->db->select("*, questions.id as question_id");
It's obvious that properties with the same name will be overriden. That's why you should assign to them 'unique' names in select statement.
You can specifically mention the ids with alies like -
$this->db->select("questions.id as question_id, users.id as user_id, questions.*, users.*");
$this->db->from("questions");
$this->db->join('users', 'users.id = questions.user_id');
$this->db->where('article_id', $articleId);
$this->db->order_by('questions.id', 'DESC');
$query = $this->db->get();
return $query->result();
This will give you specific columns named with question_id, user_id, questions table's all columns, users table's all columns
Or another better solution to use specific column names in select with alies if required.
I have a two tables in my database instructors and courses . I want to join them and for this reason wrote this code
$this->db->join('instructors', 'instructors.id = courses.instructor_id', 'left');
$query = $this->db->get_where('courses', array('courses_slug' => $slug));
return $query->row_array();
This code means:
SELECT * FROM `courses` LEFT JOIN `instructors` ON `instructors`.`id` = `courses`.`instructor_id` WHERE `courses_slug` = 'abituriyent-hazirligi'
But when I write this code to check:
$data['courses'] = $this->popular_courses_model->get_popular_courses($slug);
echo $data['courses']['id'];
die();
It writes the instructors id, not id of the course. Where can be the problem? Thanks in advance.
You are joining two table with columns of the same name ('id'). You need to be specific in your select for the columns and rename ('AS') if necessary.
select courses.id as course_id, instructor.id as instructor_id, ...
When using joins you should explicitly call out what columns you want returned like:
$select = "c.id, c.name, c.instructor_id, i.name instructor_name";
return $this->db->select($select)
//equivalent to "instructors as i"
->join('instructors i', 'i.id = c.instructor_id', 'left')
->where('c.courses_slug', $slug)
//equivalent to "courses as c"
->get('courses c')->row_array();
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');
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();
}