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();
}
Related
I have 3 tables in database:
teams
id
name
matches
id (int)
team_home_id
team_away_id
goals
id
match_id
team_id
time
I need display names of teams in view where I get goals in controller.
I know that I should do join tables.
I have this code:
public function get_goals() {
$this->db->select('goals.*');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$q = $this->db->get();
return $q->result();
}
and I don't know what next.
I need in view display names by:
$goals->team_home_name and $goals->team_away_name
To get $goals->team_home_name and $goals->team_away_name result, use aliases like this :
public function get_goals() {
$this->db->select('goals.*, home_team.name team_home_name, away_team.name team_away_name');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$this->db->join('teams home_team', 'home_team.id = matches.team_home_id');
$this->db->join('teams away_team', 'away_team.id = matches.team_away_id');
$q = $this->db->get();
return $q->result();
}
You can try with this:-
public function get_goals() {
$this->db->select('g.* , t.name as team_home_name , t.name as team_away_name);
$this->db->from('goals as g');
$this->db->join('matches as m', 'm.id = g.match_id');
$this->db->join('teams as t', 't.id = g.team_id');
$q = $this->db->get();
return $q->result();
}
U can use an alias by
guss the team_away_name and team_home_name from teams table
public function get_goals() {
$this->db->select('goals.*, team.team_home_name, team.team_away_name');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$this->db->join('teams as team', 'team.id = goals.team_id');
$q = $this->db->get();
return $q->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;
}
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 am using multiple joins but got stuck in this. I am using join of 3 tables but it fetches values of only 2 tables not 3rd one. Here my model query is:
public function seller_products()
{
$this->db->select('*')->select('wc_seller_products.id')->from('wc_seller_products')
->join('wc_seller', 'wc_seller.id = wc_seller_products.seller_id', 'LEFT')
->join('wc_seller_info', 'wc_seller_info.id = wc_seller_products.seller_id', 'LEFT');
$query = $this->db->get();
return $query;
}
It doesn't fetch values of wc_seller table .... please help
public function seller_products()
{
$this->db->select('wc_seller_products.*,wc_seller.*,wc_seller_info.*');
$this->db->from('wc_seller_products');
$this->db->join('wc_seller', 'wc_seller.id = wc_seller_products.seller_id');
$this->db->join('wc_seller_info', 'wc_seller_info.id = wc_seller_products.seller_id');
$query = $this->db->get();
return $query;
}
I am trying to select column name level_name from table levels,
which contains level_id and level_name,
for a user to know what is their level,
The table of users named as users and contain a level_id and user_id,
but I get this error ->
Column 'level_id' in on clause is ambiguous
SELECT `level_name`
FROM `levels`
JOIN `users` ON `level_id` = `level_id` WHERE `user_id` = '9'
here it is the code in the model
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels');
$this->db->join('users', 'level_id = level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
thanks in advance :)
Change the query to
SELECT `level_name`
FROM `levels` l
JOIN `users` u ON `u`.`level_id` = `l`.`level_id`
WHERE `user_id` = '9'
if you like the table name aliasing method, it is shorter and easier to read.
Or
SELECT `level_name`
FROM `levels`
JOIN `users` ON `users`.`level_id` = `levels`.`level_id`
WHERE `user_id` = '9'
If you prefere to use the full table name everywhere.
Because both tables contain a column with the name level_id the query analyser need to know which one you are addressing.
In codeigniter try
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels l');
$this->db->join('users u', 'u.level_id = l.level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
Select l.level_name
FROM levels l
JOIN users u
ON u.level_id = l.level_id
and u.user_id = '9'
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels');
$this->db->join('users', 'levels.level_id = users.level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
public function level_ownprofile($user_id)
{
$this->db->select('l.level_name');
$this->db->from('levels as l');
$this->db->join('users as u', 'l.level_id = u.level_id');
$this->db->where('l.user_id', $user_id);
$query = $this->db->get();
return $query->results();
}