Unknown column 'xxx' in 'field list', Codeigniter - php

I've been looking at this error for a while and can't see what I'm doing wrong with my codeigniter sql query, can anyone advise?
Unknown column 'album_images.album_id' in 'field list'
$this->db->select('
albums.id as album_id,
albums.album_title,
album_images.album_id,
album_images.image_id,
AVG(views.id) as views_id,
views.views as views_total,
images.id,
images.alpha_id,
images.user,
images.image_title,
images.image_type,
images.file_ext,
images.image_width,
images.image_height,
images.file_size,
images.submitted,
images.status'
);
$this->db->from('images', 'albums', 'album_images', 'views');
$this->db->join('albums', 'albums.id = album_images.album_id');
$this->db->join('views', 'views.id = images.id', 'left');
$this->db->where('albums.id', $id);
$this->db->where('images.status', 1); //fiter out deleted ones
$this->db->group_by('images.id');
$query = $this->db->get();
return $query->result();
I've checked all the spelling of everything multiple times and still get the same result. I did try reordering the this->db->from line, moving album_images to the beginning made the error change to images.id instead. If that's relevant at all.
Anyone know what I'm doing wrong here? thanks

SELECT ... FROM (`images`) JOIN `albums` ON `albums`.`id` = `album_images`.`album_id` ...
If you do a join between two tables (images, albums), you need to formulate the relationship between those two. In your case you do a join between two tables but the relationship is with a third table (album_images), so the field is not found.
See as well:
How to INNER JOIN 3 tables using CodeIgniter
Join more than two tables in codeigniter
Multiple Joins in Codeigniter

Try this you give duplicate column name
i correct it you check whether it works or not
$this->db->select('
albums.id as albums_id,
albums.album_title,
album_images.album_id,
album_images.image_id,
AVG(views.id) as views_id,
views.views as views_total,
images.id,
images.alpha_id,
images.user,
images.image_title,
images.image_type,
images.file_ext,
images.image_width,
images.image_height,
images.file_size,
images.submitted,
images.status'
);

Related

ERROR in GROUP_BY with query getting from two databases CODEIGNITER

I have a query which joins tables from two different databases. It is already showing results but then I wanted to show only unique results because some results are redundant. So I added a GROUP BY to get only the unique results but an error appears.
This is my code:
public function search_results_accommodations($location,$from_date,$to_date,$bedroom,$guests)
{
$this->db->select('*, akzapier.bookings.id as BOOKING_ID, akzapier.properties.id as PROPERTY_ID, ci_alexandrohomes.assigned_property.ID as ASSIGNED_PROPERTY_ID, ci_alexandrohomes.listings.ID as LISTING_ID');
$this->db->from('akzapier.bookings');
$this->db->join('akzapier.properties', 'akzapier.properties.id=akzapier.bookings.property_id', 'inner');
$this->db->join('ci_alexandrohomes.assigned_property', 'ci_alexandrohomes.assigned_property.property_id=akzapier.properties.id', 'inner');
$this->db->join('ci_alexandrohomes.listings', 'ci_alexandrohomes.listings.ID=ci_alexandrohomes.assigned_property.listing_id');
$this->db->where('akzapier.bookings.check_in !=', $from_date);
$this->db->where('akzapier.bookings.check_out !=', $to_date);
$this->db->where('ci_alexandrohomes.listings.city', $location);
$this->db->where('ci_alexandrohomes.listings.bedrooms', $bedroom);
$this->db->where('ci_alexandrohomes.listings.guests', $guests);
$this->db->group_by('akzapier.properties.id', 'ASC')
$query = $this->db->get();
return $query->result();
}
The error doesn't show up in the page so I converted it to SQL to see the real deal:
SELECT * akzapier.bookings.id as BOOKING_ID, akzapier.properties.id as PROPERTY_ID, ci_alexandrohomes.assigned_property.ID as ASSIGNED_PROPERTY_ID, ci_alexandrohomes.listings.ID as LISTING_ID
FROM akzapier.bookings
INNER JOIN akzapier.properties ON akzapier.properties.id=akzapier.bookings.property_id
INNER JOIN ci_alexandrohomes.assigned_property ON ci_alexandrohomes.assigned_property.property_id=akzapier.properties.id
INNER JOIN ci_alexandrohomes.listings ON ci_alexandrohomes.listings.ID=ci_alexandrohomes.assigned_property.listing_id
WHERE akzapier.bookings.check_in != '2019-09-21'
AND akzapier.bookings.check_out != '2019-09-30'
AND ci_alexandrohomes.listings.city = ‘1’
AND ci_alexandrohomes.listings.bedrooms = '2'
AND ci_alexandrohomes.listings.guests = '4'
GROUP BY akzapier.bookings.property_id ASC
ERROR SAYS:
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'akzapier.bookings.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
In general, when you use GROUP BY, your SELECT statement must contain either aggregates (such as MAX(...), COUNT(...), etc.) or the columns must appear in GROUP BY. You have selected all of the fields, non-aggregated, with the asterisk *. In this case, it's complaining about the field akzapier.bookings.id, which is neither aggregated, nor in your GROUP BY.
If you actually want unique values, try SELECT DISTINCT, which will drop duplicate rows from the result.

Laravel STDDEV with LIMIT

I'm trying to get the AVG() and the STDDEV_SAMP() on subset of data using Laravel.
So I've tried
//Data from which I want to calculate the AVG and STDDEV_SAMP()
//Limit the query to certain values
$subquery = TableAModel::join('TableB','TableA.TableB_id','=','TableB.id')
->select('Factor1')
->whereraw('conditionA <= 10')
->orderBy('DateTimeCode', 'desc')
->take('5')
->toSql();
//My aggregate functions
$aggregates = 'AVG(TableA.Factor1) as Factor1,
STDDEV_SAMP(TableA.Factor1) as Factor1_SD';
//My final query that should return the average and SD
$AVG_SD = \DB::table(\DB::raw(" ($subquery) as sub "))->select(\DB::raw("$aggregates"))->first()->toArray();
//it should return something like array([Factor1] => The_average, [Factor1_SD] => The_SD)
However, it throws me "Column not found: 1054 Unknown column 'TableA.Factor1' in 'field list'". If I try to select('*'), it throws me "Duplicate columns id".
I don't really have any experience with this but, it looks to me like your creating a view and from that view you want to find the STD and AVG of Factor1. But you say AVG(TableA.Factor1) as Factor1, I don't think TableA would be the table name for DB::table(\DB::raw(" ($subquery) as sub ")) so its not finding Factor1 because its can't find the table. would the table not be called sub.
I have no idea if this is the case or not, but hope it helps, also would've left a comment but I don't have the rep yet :(.

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

CodeIgniter PHP - How to choose which ID to select in a join while using "select *"

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.

Codeigniter Active Record "JOIN" with two Index

my tables (structures, surface) have two Index-Rows, "planet_id" and "tile_id". I want to join them, but i get a SQL-Error: "Column 'planet_id' in where clause is ambiguous".
$this->db ->select('*')
->from('structures')
->join('surface', 'structures.planet_id=surface.planet_id AND structures.tile_id=surface.tile_id')
->where('planet_id', $p->planet_id);
$query = $this->db->get();
Leads to:
Error Number: 1052
Column 'planet_id' in where clause is ambiguous
SELECT * FROM (`structures`) JOIN `surface` ON `structures`.`planet_id`=`surface`.`planet_id` AND structures.tile_id=surface.tile_id WHERE `planet_id` = '13247'
Since you have planet_id in two tables, you'll need to choose which you're applying the where to.
So, try this:
$this->db->select('*')
->from('structures')
->join('surface', 'structures.planet_id=surface.planet_id AND structures.tile_id=surface.tile_id')
->where('structures.planet_id', $p->planet_id);
$query = $this->db->get();
It might seem silly, because your join requires both planet_id's to be the same, but the where doesn't know that, and needs specific instructions.

Categories