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;
}
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 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 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
I have two tables. voting_ip and country. I want to retrieve results from country table where open_id_fk (foreign key) of voting_ip table is equal to open_id(Primary Key) of country table. How to write sql query to combine these queries and return the result. I am using the below code in my codeigniter model to retrieve number of occurances of open_id_fk in voting_ip table.
public function mostvotedans()
{
$this->db->select('open_id_fk, COUNT(*) as total');
$this->db->group_by('open_id_fk');
$this->db->order_by('total', 'desc');
$query = $this->db->get('voting_ip', 5);
return $query;
$query = $this->db->query("SELECT * FROM country WHERE open_id_fk=open_id;");
return $query;
}
change it as following.
public function mostvotedans()
{
$this->db->select('c.open_id,COUNT(ip.open_id_fk) as total')->from('voting_ip ip');
$this->db->join('country c','c.open_id=ip.open_id_fk');
$this->db->group_by('ip.open_id_fk');
$this->db->order_by('total', 'desc');
$this->db->limit(5);
$query = $this->db->get();
return $query;
}
Use a join statement :
$query = $this->db->select('v.open_id_fk, COUNT(v.*) AS total, c.country_name')
->join('country AS c', 'c.open_id = v.open_id_fk')
->from('voting_ip AS v')
->group_by('v.open_id_fk')
->order_by('total', 'DESC')
->limit(5);
Should work, I put 'country_name' because I don't know your tables.