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.
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();
}
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;
}
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
I have five table name books,categories,publications,author,reviews.
in my app user can review the book after login. so reviews table stay empty until user post a review.
I am trying to run the flowing query in codeigniter model to get book details by category_id and it's working perfect when their review is exist otherwise it's return empty array. this is happening because of this $this>db>join('reviews','reviews.book_id = books.book_id'); condition return false
how can i show result even reviews table on condition is not match?
public function get_book_details($cat_id) {
$this->db->select('*');
$this->db->from('books');
$this->db->join('categories', 'categories.id = books.category_id');
$this->db->join('publications', 'publications.id = books.publication_id');
$this->db->join('author', 'author.id = books.author_id');
$this->db->join('reviews', 'reviews.book_id = books.book_id');
$this->db->where('categories.id', $cat_id);
$query = $this->db->get();
return $query->result();
}
Use LEFT JOIN to get the result if there are no matching associations are found
$this->db->join('reviews', 'reviews.book_id = books.book_id','LEFT');
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.