Column not found: 1054 Unknown column 'locations.id' in 'on clause - php

I'm trying to load activities from a database with a specific location. I try to do that with this query:
public function selectAllActivities(){
$sql = "SELECT * FROM `activities` INNER JOIN `locations` on `activities`.`location_id` = `locations.id`";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
However when I load the website, I get this error:
Column not found: 1054 Unknown column 'locations.id' in 'on clause

That's a typo, but I can't explain it in comment because of the backticks.
This :
`locations.id`
Is meant to be
// v-v------- Notice the backticks
`locations`.`id`

Have you tried
`activities`.`location_id` = `locations`.`id`
Inner joins should read like
ON table1.column_name = table2.column_name;
You can read more at this URL

Related

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.

Laravel Query Builder join doesn't affect query

I'm trying to build in search functionality in my application (Laravel 5.1), but my join doesn't seem to do anything to the resulting query. What am I doing wrong?
Code:
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);
$query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));
$invoices = $query->paginate(15);
Resulting query:
select count(*) as aggregate from invoice_headers where customer_code_id = 1 and (invoice_types.name <> 380)
Resulting response:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'invoice_types.name' in 'where clause'
This is the query I was hoping to see:
select count(*) as aggregate
from invoice_headers
inner join invoice_types
on invoice_headers.invoice_type_id = invoice_types.id
where customer_code_id = 1
and (invoice_types.name <> 380)
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);
you need to store the query in a variable.
$query = $query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));
$invoices = $query->paginate(15);
You need to add JOIN with invoice_types table if you want to filter on invoice_types.name.

yii multiple inner joins

I am trying to write a query in yii. I have the following which works
$criteria = new CDbCriteria;
$criteria->condition = "($column = :id)";
$criteria->params = array(":id" => $id );
$rows = Jobs::model()->with('pROJ')->findAll($criteria);
This returns the model of Jobs in array. I need to write the following query in yii to return a model
SELECT jobs.JOBNO, jobs.STATUS, projects.ORDERNO, jobs.PROJID, jobs.NAME, jobs.SEQ, jobs.PCENTDONE, jobs.EARNED, jobs.VALUE, jobs.DATEIN, jobs.DATEDONE, jobs.DATEDUE, jobs.SENTBACK, jobs.ORIGTAPES, jobs.COMMENTS, projects.CATEGORY, orders.BIDNO
FROM (jobs INNER JOIN projects ON jobs.PROJID = projects.PROJID) INNER JOIN orders ON projects.ORDERNO = orders.ORDERNO
where jobs.projid = 3002001
ORDER BY jobs.JOBNO, jobs.PROJID
I have tried the following but it does not work
$rows = Yii::app()->db->createCommand()
->select('jobs.*, projects.ORDERNO, projects.CATEGORY, orders.BIDNO')
->from('jobs, projects, orders')
->join('projects p','jobs.PROJID = p.PROJID')
->join('orders o', 'p.ORDERNO = o.ORDERNO')
->where('jobs.projid=:id', array(':id'=>$id))
->queryRow();
I get the following error
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jobs.PROJID' in 'on clause'. The SQL statement executed was: SELECT `jobs`.*, `projects`.`ORDERNO`, `projects`.`CATEGORY`, `orders`.`BIDNO`
FROM `jobs`, `projects`, `orders`
JOIN `projects` `p` ON jobs.PROJID=p.PROJID
JOIN `orders` `o` ON p.ORDERNO=o.ORDERNO
WHERE jobs.projid=:id
I have updated to
$rows = Yii::app()->db->createCommand()
->select('jobs.*, projects.orderno, projects.category, orders.bidno')
->from('jobs')
->join('projects p','jobs.projid = p.projid')
->join('orders o', 'p.orderno = o.orderno')
->where('jobs.projid=:id', array(':id'=>$id))
->queryRow();
but i still get the error. All columns in mysql are CAPS
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'projects.orderno' in 'field list'. The SQL statement executed was: SELECT `jobs`.*, `projects`.`orderno`, `projects`.`category`, `orders`.`bidno`
FROM `jobs`
JOIN `projects` `p` ON jobs.projid = p.projid
JOIN `orders` `o` ON p.orderno = o.orderno
WHERE jobs.projid=:id
As #DCoder said: your select should now read select('jobs.*, p.orderno, p.category, o.bidno'). For consistency you should also alias jobs as follows
$rows = Yii::app()->db->createCommand()
->select('j.*, p.orderno, p.category, o.bidno')
->from('jobs j')
->join('projects p','j.projid = p.projid')
->join('orders o', 'p.orderno = o.orderno')
->where('j.projid=:id', array(':id'=>$id))
->order('j.jobno,j.projid')
->queryRow();
I think you should remove projects, orders from ->from('jobs, projects, orders') and may be you should lower the case of jobs.PROJID as your error message says that it couldn't find the column.

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

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

Unknown column error produced by Doctrine Query Where clause

I have the following query:
return Doctrine_Query::create()
->from('Model_Article m')
->where($where)
->orderBy($order);
In $where I have this:
$where='m.title='.$name;
The above produces this error:
Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sadsa' in 'where clause'. Failing Query: "SELECT COUNT(*) AS num_results FROM article a WHERE a.title = sadsa"
Why?
Error is because resulting query looks like "** where title=sadsda **", so SQL engine looks for column named "sadsda". To prevent it you must mention, that you want to compare with a string, not a column value. You can make it using your engine rules (usually enclose string with "'"), but it isn't secure, I think you should use placeholders engine, that doctrine provides, for example
$whereKey = 'm.title=?';
$whereValue = 'sadsda';
Doctrine_Query::create()->from('Model_Article m')->where($whereKey,$whereValue)->orderBy($order);

Categories