How to write this query in yii? - php

SELECT business.id,business.business_name,address.city FROM business INNER JOIN address ON business.id=address.business_id WHERE business.business_name like '%monal%' and address.city='islamabad'
Where monal and islamabad would be a value coming from a form.
when i replace islamabad with a variable it gives me error.
My query in yii.
$user = Yii::app()->db->createCommand()
->select('business.id,business_name,business.image,business.business_description,address.city')
->from('business')
->join('address', 'business.id=address.business_id')
//->where(array('like', 'business.business_name', '%'.$name.'%'))
->where(array('and', 'address.city=$city', array('like', 'business.business_name', '%'.$name.'%')))
->queryALL();
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Islamabad' in 'where clause'. The SQL statement executed was: SELECT `business`.`id`, `business_name`, `business`.`image`, `business`.`business_description`, `address`.`city`
FROM `business`
JOIN `address` ON business.id=address.business_id
WHERE (address.city=Islamabad) AND (`business`.`business_name` LIKE '%nan%')

Try this as your query instead
$results = Yii::app()->db->createCommand()
->select('b.id, b.business_name, a.city')
->from('business b')
->join('address a', 'b.id = a.business_id')
->where('b.business_name LIKE :businessName AND a.city = :city', array(
':businessName' => '%' . $businessNameVariable . '%',
':city' => $cityVariable,
))
->queryAll();

Related

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

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

YEAR(begin_date) unknown column in laravel

SELECT COUNT(*) as count, MONTH(begin_date)
FROM `events`
WHERE (YEAR(begin_date) = YEAR(CURDATE()))
OR (YEAR(begin_date) = YEAR(CURDATE()) + 1)
GROUP BY MONTH(begin_date)
Here is sql query, i want to write it in laravel eloquent.
what i try:
$oncoming_events = DB::table('events')
->select(DB::raw('count(*) as numOfOncomingEvents, MONTH(begin_date)'))
->where('YEAR(begin_date)', '=', 'YEAR(CURDATE())')
->orWhere('YEAR(begin_date)', '=', 'YEAR(CURDATE()) +1')
->groupBy('MONTH(begin_date)')->get();
Error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'YEAR(begin_date)' in 'where clause' (SQL: select count(*) as numOfOncomingEvents, MONTH(begin_date) from events where YEAR(begin_date) =
laravel 5.6
btw
sql query works..
You need to use DB::raw() in where to tell the query builder that it is not column name it is data manipulation in query,
$oncoming_events = DB::table('events')->select(DB::raw('count(*) as numOfOncomingEvents, MONTH(begin_date)'))->where(DB::raw('YEAR(begin_date)'), '=', 'YEAR(CURDATE())')->orWhere(DB::raw('YEAR(begin_date)'), '=', 'YEAR(CURDATE()) +1')->groupBy(DB::raw('MONTH(begin_date)'))->get();

Yii Create Command can't get hard coded value from select

This is my query
$result = Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)
->select('t.id, "contract" AS type')
->from('test t')
->where('t.company_id=1')
->queryAll();
Am getting this error,
Column not found: 1054 Unknown column '"contract"' in 'field list'. The SQL statement executed was: SELECT `t`.`id`, `"contract"` AS `type` FROM `test` `t` WHERE t.company_id=1
try using avoid literal name eg:
$result = Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)
->select('t.id, case when 1 = 1 then 'contract' end AS type')
->from('test t')
->where('t.company_id=1')
->queryAll();
Working with minor additional change
$result = Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)
->select('t.id, (case when 1 = 1 then "contract" end) AS type')
->from('test t')
->where('t.company_id=1')
->queryAll();

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.

Categories