Inner join query in codeigniter - php

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;
}

Related

Join more than 2 database table in codeigniter Error : 1066

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();
}

Show data record using id in codeigniter

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.

Codeigniter how to avoid duplicates showing one to many relationship

I have been trying to show rows without duplicates but the query isn't working properly. I think the problem is one to many relationship, because one 'intervaloHorario' has many 'citas'. So, for example, i want to show only: 'From 8:00 to 15:00 (this is an intervaloHorario)' to date (cita) '27/08/1988'. What should i do?
Controller
$this->Fechacita_Model->delete_duplicaterow();
Model
public function delete_duplicaterow() {
$this->db->select('
c.intervaloHorario','ci.cita'
);
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario','left');
$this->db->group_by('c.idIntervaloHorario','ci.cita');
$query = $this->db->get();
return $query->num_rows();
}
Model(EDIT)
$this->db->select(array('c.intervaloHorario', 'ci.cita'));
$this->db->distinct();
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario', 'left');
$this->db->group_by('c.idIntervaloHorario', 'ci.cita');
$query = $this->db->get();
$this->db->last_query();
return $query->num_rows();
Database
Current database
Screenshot
Current list (unordered list but duplicates persists)
You can use $this->db->distinct() and add selecting primary key to remove duplicate:
public function delete_duplicaterow() {
$this->db->select(array('c.intervaloHorario', 'ci.cita'));
$this->db->distinct();
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario','left');
$this->db->group_by('c.idIntervaloHorario','ci.cita');
$query = $this->db->get();
return $query->num_rows();
}
Use the keyword DISTINCT in your query
reference : https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html

Join two MySQL columns and output colB.name where colA.id is equal to colB.id

I'am using CodeIgniter active records and I have a lack in knowledge of Joining multiple table columns in MySQL.
What I basically want to do, is to output all rows from a ci_categories table, but instead of showing ci_categories.component_id as a numeric, i wish to output the ci_components.name which have equal (or same) ci_components.id from another table ci_components by using the Active Records in CodeIgniter.
What I have already done, but with errors is:
Model:
public function getItemName($id){
$this->db->select('*');
$this->db->where('id', $id);
$result = $query->result();
return $result;
}
and View:
<?php echo $this->component_model->getItemName($cat['category']->com_id);?>
Any tips pls ?
$this->db->select('name')
->from('ci_components')
->where('ci_categories.component_id = ci_componenents.id');
$query = $this->db->get();
or
$this->db->select('name');
$this->db->from('ci_components');
$this->db->join('ci_categories', 'categories.component_id = ci_components.id');
$query = $this->db->get();
https://www.codeigniter.com/userguide2/database/active_record.html#select

How to select all field from table with distinct (field x) ?? Codeigniter

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();
}

Categories