Here I have function index which is having two different model calls as follows:
CONTROLLER:
function index()
{
$people = $this->M_results->search_people(); // first model method call
// print_r($people); //shows result
$skills = $this->M_results->get_skill($category_name); // second model method call
}
MODEL:
function search_people()
{
$this->db->select("registration,gender,profile_img,location");
$this->db->from('search_result');
$this->db->join('services','search_result.registration = services.reg_id','left');
$this->db->join('review','search_result.registration = review.reg_id AND review.active = 0','left');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
function get_skill($category_name)
{
$this->db->select('GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id,GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill,sub_categories.name as sub_cat,class');
$this->db->from('skills');
$this->db->join('sub_categories','skills.sub_cat_id = sub_categories.id');
$this->db->join('categories','sub_categories.cat_id = categories.id');
$this->db->where('categories.name',$category_name);
$this->db->where('skills.active',0);
$this->db->group_by('sub_categories.name');
// echo $this->db->get_compiled_select(); exit();
$query = $this->db->get();
return $query->result();
}
Now the problem is that, the call to search_people is returning exact result.
But on running the second model call i.e to get_skill function, the select query of get_skill is including the columns of select query in search_result function. And shows the below database error:
Error Number: 1054
Unknown column 'registration' in 'field list'
SELECT registration, gender, profile_img, location,GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id, GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill, sub_categories.name as sub_cat, class FROM skills JOIN sub_categories ON skills.sub_cat_id = sub_categories.id JOIN categories ON sub_categories.cat_id = categories.id WHERE categories.name = 'Child and Pet Care' AND skills.active =0 GROUP BY sub_categories.name
In the above query you can see both columns in search_people and get_skill.
Posting the answer for future references.
Resets the current Query Builder state. Useful when you want to build a query that can be cancelled under certain conditions.
$this->db->reset_query();
I found the solution from below link:
https://www.codeigniter.com/userguide3/database/query_builder.html#CI_DB_query_builder::reset_query
Related
so i kinda new with codeigniter and im trying to join 3 table from my database
database 1 : dkm (id, tgl, ref, etc)
database 2 : order_product (kode_barang, packing, nama_barang, etc)
database 3 : product (kodeprod, tglpakai, etc)
im already trying what other ppl do to join more than 2 table in codeigniter but i got this error :
Error Number: 1066
Not unique table/alias: 'order_product'
SELECT *
FROM `order_product`
JOIN `order_product` ON `order_product`.`kode_barang` = `dkm`.`id`
JOIN `order_product` ON `order_product`.`kode_barang` = `produksi`.`kodeprod`
This is my code :
Bukaka_model.php
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('order_product','order_product.kode_barang = dkm.id');
$this->db->join('order_product','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
You're trying to join to the same table multiple times, instead you need to join to the other tables once each.
You just need to change the names of the table you're joining to:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
Try this,
Here, you have a mistake in joining tables, in CI join() in the first parameter you need to pass/write table name with you want to join
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}else{
return array();
}
}
Try this:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','dkm.id= order_product.kode_barang');
$this->db->join('produksi',' produksi.kodeprod = order_product.kode_barang');
$query = $this->db->get();
return $query->result();
}
i got this error message
Column 'id_siswa' in where clause is ambiguous
SELECT * FROM `siswa` `a`
LEFT JOIN `pembayaran_spp` `b` ON `b`.`id_siswa`=`a`.`id_siswa`
WHERE `id_siswa` = '7%E2%80%8B'
I have 2 table.
1.table 'siswa'
structure(id_siswa,nama_siswa,id_tahun_masuk)
2.table 'pembayaran_spp'->
structure(id_pembayaran,id_siswa,jml_pembayaran,id_tahun,date)
I want to show data 'pembayaran_spp' by id_siswa.
so when i click detail on 'siswa', data 'pembayaran' is showed by id_siswa.
My Controller
function detailtagihan($id_siswa)
{
$data['siswa'] = $this->M_keuangan->tagihansiswa($id_siswa);
$this->load->view('template/header');
$this->load->view('template/sidebar');
$this->load->view('keuangan/v_detailtagihan',$data);
$this->load->view('template/footer');
}
My Model
function tagihansiswa($id_siswa)
{
//$data = array('id_siswa' => $id_siswa );
$this->db->select('*');
$this->db->from('siswa a');
$this->db->join('pembayaran_spp b','b.id_siswa=a.id_siswa', 'left');
$this->db->where('id_siswa',$id_siswa);
$query = $this->db->get();
if($query->num_rows()>0)
return $query->result();
}
You should mention of which table you want to use the column id_siswa in your where clause. As both of the tables are having a column with same name, you are getting this error.
If you want to use siswa then in your where condition write a.id_siswa
And if you want to use pembayaran_spp then in your where condition b.id_siswa.
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...
error msg: Unknown column 'category' in 'where clause'
i have to inner join two tables. what should be the correct query?
Or what parameter should i put in $query->get()? If I only put 'film', it cannot find 'category' column in another table.
$query = $this->db->select('title, name as category, rental_rate, length')->order_by($sort_by, $sort_order);
$query = $this->db->join('film_category', 'film_category.film_id = film.film_id');
$query = $this->db->join('category', 'film_category.category_id = category.category_id');
if(strlen($query_array['title'])) {
$query->like('title', $query_array['title']);
}
if(strlen($query_array['category'])) {
$query->where('category', $query_array['category']);
}
$data['films'] = $query->get('film', 20, $this->uri->segment(6));
$this->db->select('title, name as category, rental_rate, length')->order_by($sort_by, $sort_order);
$this->db->from('film'); /*I assume that film was the table name*/
$this->db->join('film_category', 'film_category.film_id = film.film_id');
$this->db->join('category', 'category.category_id = film_category.category_id');
$query = $this->db->get();
var_dump($query);
Double check that code I added and make sure that on category table, the column is called category_id, and not just id, and that under film_category, there's a category_id column.
If with the code I submitted, you still get the error, try to replace the first line with
$this->db->select('title, name, rental_rate, length')->order_by($sort_by, $sort_order);
I'm not sure if using a name that matches a table will cause a trouble with CodeIgniter and ActiveRecord.
Hope that helps.
I have 3 tables in my database :-
tbl_roles(role_id,role_name);
tbl_users(id,role_id,username,email,password);
tbl_tickets_replies(id,ticket_id,user_id,role_id,comments)
role_id, id, id are primary keys of corresponding tables.
i need :-
username from tbl_users.
role_name from tbl_roles.
comments from tbl_tickets
where ticket_id from tbl_tickets_replies = $ticket_id coming as a parameter.
My Model Function is :-
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->from('tbl_tickets_replies');
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
$comments = $this->db->get('tbl_tickets_replies');
return $comments;
}
this is showing database error i.e., I am doing wrong query.
I want to ask how can I join three tables to fetch data from 3 different tables
This error is showing :-
A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'tbl_tickets_replies'
SELECT tbl_tickets_replies.comments, tbl_users.username,
tbl_roles.role_name FROM (tbl_tickets_replies,
tbl_tickets_replies) JOIN tbl_users ON tbl_users.id =
tbl_tickets_replies.user_id JOIN tbl_roles ON
tbl_roles.role_id=tbl_tickets_replies.role_id WHERE
tbl_tickets_replies.ticket_id = '6'
Filename: C:\wamp\www\local.helpdesk.com\bonfire\codeigniter\database\DB_driver.php
Line Number: 330`
You are referring to tbl_tickets_replies twice.
Try this:
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,
tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
return $this->db->get('tbl_tickets_replies');
}
For complex queries, i prefer using the plain SQL as follows.
$sql = "SELECT....";
$q = $this->db->query($sql);
Btw, try removing the table name from db->get function
$comments = $this->db->get(); //change this
Join with condition.
$this->db->select('*'); $this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
$this->db->where(array('category.id' => 10)); $query =
$this->db->get();