join 3 tables in mysql codeigniter - php

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

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

codeigniter inner join tables syntax

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.

Query for getting values from multiple tables

I have a table video with fields videoid, genre(int-foreign key), language(int-foreign key) etc. I want to get the value of genre from genre table and language from movie_languages table.
The structure of tables are given below:
video
genre
movie_languages
How can I join these 3 tables to get the language and genre related to each videos in video table. Also when user didn't select genre/language in the form, value 0 will be inserted to the table. Will this affect the query. I am using codeingiter and I tried with the following query and is not working.
$this->db->select('video.*,movie_languages.language,genre.genre');
$this->db->join('genre', 'video.genre = genre.id');
$this->db->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();
Please help me.
Thanks in advance.
It will be, you need a left join in this case
Try this
$query = $this->db
->select('v.*,ml.language,g.genre')
->from('video as v')
->join('movie_languages AS ml', 'v.language = ml.id', 'left outer')
->join('genre AS g', 'v.genre = g.id', 'left outer')
->get();
try this
$this->db->select('video.*,movie_languages.language,genre.genre')
->from('video')
->join('genre', 'video.genre = genre.id')
->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();

Retrieving multiple values associated with a single record. MySQL PHP Codeigniter

I need help with a query on following tables.
table user(
user_id char(5),
username char(12),
columnx char(10));*
table phone(
user_id char(5),
phone_number number(10),
primary(Y, N) char(1));*
Both tables linked on user_id,
Here each user can have multiple phone_numbers.
I need to pullout a set of users and along with their phone numbers. I am trying to do the following.
in model
$this->db->where('columnx', 'something');
$query = $this->db->get('users');
foreach($query->result() as $row) {
$this->db->select('phone_number');
$this->db->where('user_id', $row->user_id);
$this->db->where('primary', 'Y');
$q = $this->db->get('phone');
}
How do I return from model and display multiple phone number for each user, when I my first $query returns multiple users??
Thanks in advance,
Prim
Try using join:
$query = $this->db->select('*')
->from('user as a')
->join('phone as b', 'a.user_id = a.user_id')
->where(array('a.columnx' => 'something'));
->get();
var_dump($query->result());

Codeigniter active record : how to read the joined table

i have some table, and the relationship goes like this
and i want to get all the record of those 2 tables. so i use this query in my model
$this->db->select('*');
$this->db->from('ms_Kategori_Material.*,ms_Material_Jasa.*');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
$table = $this->db->get();
return $table;
and then i got error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*, `ms_Material_Jasa`.*) JOIN `ms_Material_Jasa` ON `ms_Kategori_Material`.`Kode' at line 2
SELECT * FROM (`ms_Kategori_Material`.*, `ms_Material_Jasa`.*) JOIN `ms_Material_Jasa` ON `ms_Kategori_Material`.`Kode_Kategori_Material_Jasa` = `ms_Material_Jasa`.`Kode_Kategori_Material_Jasa`
why do i can't read the needed table ?
in your from, there shouldnt be a .*,
i.e
$this->db->from('ms_Kategori_Material','ms_Material_Jasa');
and if your adding the table to join, no need to add it to from.
so it becomes
$this->db->from('ms_Kategori_Material');
$this->db->join('ms_Material_Jasa','....');
final query:
$this->db->select('*');
$this->db->from('ms_Kategori_Material');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
$table = $this->db->get();
return $table;
You may try this (to join and select fields from both tables)
$this->db->select('ms_Kategori_Material.*, ms_Material_Jasa.*');
$this->db->from('ms_Kategori_Material');
$this->db->from('ms_Material_Jasa');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$table = $this->db->get();
return $table->result();
$this->db->select('*');
$this->db->from('ms_Kategori_Material'); // full table name
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
//print_r($this->db->last_query()); display raw sql
$print_r($result->result_array());
Read more # http://codeigniter.com/user_guide/database/active_record.html

Categories