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();
}
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();
}
I have two tables: one is users and other is user_profile and in users table suppose 20 row and user_profile table only two rows.
How could I get all user which has profile or whom has no profile in codeingniter?
You can use join as below
Note : user_id is primary key of users table
$this->db->select('*');
$this->db->join('user_profile up', 'up.user_id = u.user_id', 'left');
$this->db->get('users u')->result_array();
To get the users with profile, use INNER JOIN
public function users_profile(){
$this->db->select('users.*, user_profile.column1 as column1, user_profile.column2 as column2');
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'inner');
$query = $this->db->get('users');
return $query->result_array();
}
To get all users with or without profile, use LEFT JOIN
public function all_users(){
$this->db->select('users.*, user_profile.column1 as column1, user_profile.column2 as column2');
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'left');
$query = $this->db->get('users');
return $query->result_array();
}
Rememer to make user_id the PRIMARY KEY for both tables
I'm new to Codeigniter and PHP, just learning. I've searched for this but couldn't find the right answer. I have a table that contain some data, lets call it posts and in this table, each post have a category_id. I have another table called categories and in this table I have 2 columns: id and name.
I want to join the two tables that the result I'll get is the same as posts with another column called category_name that will be taken from categories.name. Just can't figure out how to do so.
This is what I was up to so far:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id == categories.id');
$query = $this->db->get();
return $query->result_array();
}
Thanks in advance.
========================================================================
EDIT:
After trying Adrian Forsius answer:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*, category.name AS category_name');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
I'm getting this error:
Error Number: 1054: Unknown column 'category.name' in 'field list'
SELECT *, `category`.`name` AS category_name
FROM (`posts`)
JOIN `categories` ON `posts`.`category_id` = `categories`.`id`
WHERE `category_id` IS NULL
ORDER BY `id` desc
LIMIT 20
====
EDIT 2:
So I found some mistakes in the code and fixed them, tried again, but still I get an error: (The mistakes were using $categoryID instead of $categoryId and select category.name instead of the true name of the table categories which is categories.name
Error Number: 1052: Column 'id' in order clause is ambiguous
SELECT *, `categories`.`name` AS category_name
FROM (`posts`)
JOIN `categories` ON `posts`.`category_id` = `categories`.`id`
WHERE `category_id` = '3'
ORDER BY `id` desc
LIMIT 20
This should do what you are looking for:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*, categories.name AS category_name');
$this->db->from('posts')->order_by('posts.id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryId);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
replace
$this->db->select('*');
by
$this->db->select('posts.*');
$this->db->select('categories.name','category_name');
CI's documentation is pretty good.
Try this I think you forget to place = in place of ==
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
I'm trying to get an entire row values from a new SQL query so.
How can I make a function
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
to something like this :
In model page
public function getID_researcher($lastname){
$query = $this->db->get_where('researcher', array('lastname' => $lastname));
return $query->row_array();
}
I need to return one row result base on the lastname which is from table1
To clarify here, 'researcher' is the table where it gets data but what I want is the new SQL for me to get the data.
I tried this one but still error.
public function getID_researcher($lastname){
$sql = = $this->db->query('Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id');
$query = $this->db->get_where($sql, array('lastname' => $lastname));
return $query->row_array();
}
to use this query
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
you can try to use active record db 'join' like this:
$this->db->from('table1 t1');
$this->db->join('table2 t2', 't1.t1_id = t2.t1_id', 'left');
$this->db->join('table3 t3', 't3.id = t2.t3_id', 'left');
$this->db->where('lastname', $lastname);
$query = $this->db->get();
$c = 0;
foreach($query->result() as $q){
$result[$c]['name'] = $q->lastname;
// put move variable here
$c++;
}
return $result;
hope this help