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.
Related
Here I have function index which is having two different model calls as follows:
CONTROLLER:
function index()
{
$people = $this->M_results->search_people(); // first model method call
// print_r($people); //shows result
$skills = $this->M_results->get_skill($category_name); // second model method call
}
MODEL:
function search_people()
{
$this->db->select("registration,gender,profile_img,location");
$this->db->from('search_result');
$this->db->join('services','search_result.registration = services.reg_id','left');
$this->db->join('review','search_result.registration = review.reg_id AND review.active = 0','left');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
function get_skill($category_name)
{
$this->db->select('GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id,GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill,sub_categories.name as sub_cat,class');
$this->db->from('skills');
$this->db->join('sub_categories','skills.sub_cat_id = sub_categories.id');
$this->db->join('categories','sub_categories.cat_id = categories.id');
$this->db->where('categories.name',$category_name);
$this->db->where('skills.active',0);
$this->db->group_by('sub_categories.name');
// echo $this->db->get_compiled_select(); exit();
$query = $this->db->get();
return $query->result();
}
Now the problem is that, the call to search_people is returning exact result.
But on running the second model call i.e to get_skill function, the select query of get_skill is including the columns of select query in search_result function. And shows the below database error:
Error Number: 1054
Unknown column 'registration' in 'field list'
SELECT registration, gender, profile_img, location,GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id, GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill, sub_categories.name as sub_cat, class FROM skills JOIN sub_categories ON skills.sub_cat_id = sub_categories.id JOIN categories ON sub_categories.cat_id = categories.id WHERE categories.name = 'Child and Pet Care' AND skills.active =0 GROUP BY sub_categories.name
In the above query you can see both columns in search_people and get_skill.
Posting the answer for future references.
Resets the current Query Builder state. Useful when you want to build a query that can be cancelled under certain conditions.
$this->db->reset_query();
I found the solution from below link:
https://www.codeigniter.com/userguide3/database/query_builder.html#CI_DB_query_builder::reset_query
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();
Basically, I've got this coding convention that any primary key which is an ID, I will call the column name "id". So here comes my problem. I'm joining two tables and I'm getting the ID of the second table instead of the first table. I know if I use select "artists.id, ..." it will work, but I want to know if there's a fix with using "select *" which would be better for future expansion (new colums will come ...).
Here's my model:
$this->db->select('*');
$this->db->from('artists');
$this->db->join('categories', 'artists.category_id = categories.id');
$this->db->where('id', $id);
$this->db->limit(1);
With Print_R I can see I'm getting all columns (but only 1 id, which is from the categories table instead of artists table) without any table prefix.
You should qualify your columns with a table alias
$this->db->select('a.id as artist_id, c.id as category_id, a.column2,c.column3');
$this->db->from('artists a');
$this->db->join('categories c', 'a.category_id = c.categories.id');
$this->db->where('a.id', $id);
$this->db->limit(1);
If you want to continue using SELECT *
$this->db->select('a.*, c.*, a.id as artist_id, c.id as category_id');
$this->db->from('artists a');
$this->db->join('categories c', 'a.category_id = c.categories.id');
$this->db->where('a.id', $id);
$this->db->limit(1);
Keep in mind, that the LAST duplicate column will be returned. So, a.*,c.* will return c.id as id and c.*,a.* will return a.id as id.
I think to save you trouble and for the future, always use the table in front of the column name.
There is no logic here, when you look for * it means all fields, in Oracle for example you will get all fields with the table in front, i guess in MySQL it doesn't, but if i were you, i would not risk it.
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();
I'm new to PHP/MySQL and super-new to CodeIgniter..
I have information in many MySQL tables. I want to retrieve it with JOIN where the tables primary keys are equal to $variable... How can I do it and get all the fields without the primary key field???
What I'm doing now is this (only two tables joined here):
function getAll($id) {
$this->db->select('*');
$this->db->from('movies');
$this->db->join('posters', 'movies.id= posters.id');
// WHERE id = $id ... goes here somehow...
$q = $this->db->get();
if ($q->num_rows() == 1) {
$row = $q->row();
$data = array(
'id' => $row->id,
'title' => $row->title,
'year' => $row->year,
'runtime' => $row->runtime,
'plotoutline' => $row->plotoutline,
'poster_url' => $row->poster_url
);
}
$q->free_result();
return $data;
id (PK), title, year, runtime and plotoutline are columns from the first table and poster_url is a field from the second table. The second table also contains an ID (PK) column that I don't want to Retrieve because I already have.
Jon is right. Here's an example:
$this->db->select('movies.id,
movies.title,
movies.year,
movies.runtime as totaltime,
posters.poster_url');
$this->db->from('movies');
$this->db->join('posters', 'movies.id= posters.id');
$this->db->where('movies.id', $id);
$q = $this->db->get();
This will return objects that have ->id, ->title, ->year, ->totaltime, and ->poster_url properties. You won't need the additional code to fetch the data from each row.
Don't forget, if the Active Record syntax gets a little unwieldy, you can use full SQL queries and get the same results:
$sql = "SELECT movies.id,
movies.title,
movies.year,
movies.runtime as totaltime,
posters.poster_url
FROM movies
INNER JOIN posters ON movies.id = posters.id
WHERE movies.id = ?"
return $this->db->query($sql, array($id))->result();
Both forms will ensure that your data is escaped properly.
CodeIgniter Active Record
Query Binding in CodeIgniter
An asterisk will return all the fields. To return a subset of these, i.e. all fields apart form the repeated id field, simply list the columns which you require rather than use '*'.
It is often a good idea to not use asterisk anyway. In the future of the app, someone may add a large field to the table which will be surplus to your requirements, and will slow your queries.
Simply put with method chaining:
$this->db->select('*')
->from('movies')
->join('posters', 'movies.id= posters.id')
->where('movies.id', $id)
->get();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();