I have the following query:
SELECT * FROM `alerts` WHERE `title` LIKE `%template%`
This should return at least 3 results with titles that includes the word 'template' but I'm getting the following error: -
1054 - Unknown column '%template%' in 'where clause'
As far as I can tell its syntactically correct and calling the correct column names. What am I missing?
Use single quotes for the %template%:
SELECT * FROM `alerts` WHERE `title` LIKE '%template%'
Related
I'm using the latest Laravel 5.4
I am trying to make a simple query to search users by name. The query written for MySQL looks like this:
SELECT *
FROM users
WHERE upper(name) LIKE '%FOO%';
I'm trying to make it work with Eloquent. Things I've tried but failed:
User::where('upper(name)', 'LIKE', '%FOO%')->get()
DB::table('users')->where('upper(name)', 'LIKE', '%FOO%')->get()
Both fail with the following error:
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'upper(name)' in 'where clause' (SQL: select * from users where upper(name) LIKE %FOO%)'
The query seems to fail because Eloquent wraps the upper(email) statement with backticks (" ` ", " ` "). Is there a way to go around this issue or do I have to use a particular eloquent function to get convert a column to uppercase, lowercase, e.t.c?
Use DB::raw()
User::where(DB::raw('upper(name)'), 'LIKE', '%FOO%')->get()
It would generate query like this
"select * from `users` where upper(name) LIKE ?"
You can use whereRaw() in laravel to achieve this :
User::whereRaw("upper(name) LIKE '%FOO%'")
->get();
I have written a sql query using Laravel, but I am not understanding, why the error is creating! The codes are given below,
The join operation:
MeetingRoom::select('mr_id')
->leftJoin('meetingroomhistory',function($join)
{
$join->on('country','=',Session::get('country'));
$join->on('location','=',Session::get('location'));
$join->on('building','=',Session::get('building'));
$join->on('floor','=',Session::get('floor'));
$join->on('name_of_mr','=',Session::get('room'));
})
->where('meetingroom.id','=','meetingroomhistory.mr_id')
->get();
The error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Bangladesh' in
'on clause' (SQL: select `mr_id` from `meetingroom` left join
`meetingroomhistory` on `country` = `Bangladesh` and `location` =
`Dhaka` and `building` = `Uttara` and `floor` = `3` and `name_of_mr` =
`1` where `meetingroom`.`id` = meetingroomhistory.mr_id)
But if I run the query manually to change this syntax `Bangladesh` to "Bangladesh" using phpmyadmin so it runs well and show me the result. Please someone let me know, how can I fix it?
I think you've confused the where part and the join part.
$join->on($x, $y) statements are for connecting your tables together. Both $x and $y should be names of columns in your tables - that is why Laravel is adding backticks to them.
$where() statements are for comparing with values that you supply. Your Session::get() calls indicate that's what you're doing.
Here's a suggested rewrite, but it may need to be tweaked to suit your exact needs.
MeetingRoom::select('mr_id')
->leftJoin('meetingroomhistory', 'meetingroom.id', '=', 'meetingroomhistory.mr_id')
->$where('meetingroom.country','=',Session::get('country'))
->$where('meetingroom.location','=',Session::get('location'))
->$where('meetingroom.building','=',Session::get('building'))
->$where('meetingroom.floor','=',Session::get('floor'))
->$where('meetingroom.name_of_mr','=',Session::get('room'))
->get();
I assumed the country, location, etc. columns are part of the meetingroom table, if not you can just change them to $where('meetingroomhistory.country', ...) and so on instead.
Finally, I'm not sure why you're selecting only one value, but of course, that's up to you. If you need only a single cell, you can use pluck(), and if you need a lot of single results you could do lists().
I think you should try this.
MeetingRoom::select('mr_id')
->join('meetingroomhistory', function($join)
{
$join->on('meetingroom.id','=','meetingroomhistory.mr_id')
->where('meetingroomhistory.country','=',Session::get('country'));
->where('meetingroomhistory.location','=',Session::get('location'));
->where('meetingroomhistory.building','=',Session::get('building'));
->where('meetingroomhistory.floor','=',Session::get('floor'));
->where('meetingroomhistory.name_of_mr','=',Session::get('room'));
})
->get();
I'm running the following SQL in Laravel:
$sql = 'SELECT university.id, university.name, MAX(uni_score) AS score
FROM (SELECT uni_id, place AS uni_score FROM ranking) AS tmp
LEFT JOIN university ON university.id = tmp.uni_id
ORDER BY score';
$result = DB::select(DB::raw($sql));
However the code throws this error:
Column not found: 1054 Unknown column 'uni_score' in 'field list'.
uni_score is an alias for place field in ranking table. The query above works fine when ran directly in phpMyAdmin.
What am I doing wrong?
Try using the DB::statement($query); method.
I Have the following code of php
$query = sprintf("SELECT to_go.to_location FROM to_go
INNER JOIN to_location ON to_go.to_location_id = to_location.id
WHERE match(to_location ) against(%s)", mysql_real_escape_string($location));
i tried every thing but it keep output me that following error "Unknown column in 'where clause ?" i tried to change the names of the columns and still the same problem
match(to_location ) against needs to be provided a field, not a table:
match(to_location.id) against(something)
I guess you may need to replace
WHERE match(to_location )
with
WHERE match(to_go.to_location)
Since you have a column name the same name as a table name, MySql is probably confusing them and thinking match(to_location) refers to the table. Try using the fully-qualified column name, i.e., table_name.column_name.
Query:
select * from table_name ORDER BY name like 'C%' DESC;
This query work fine in MySql, but i am unable build the query using Zend DB.
I am getting error while executing.
PHP DB code:
$result = $this->getDefaultAdapter()
->select()
->from($this->_name,array('*'))
->order("name like 'C%' DESC")
->query()
->fetchAll();
Error:
Column not found: 1054 Unknown column 'name like 'C%'' in 'order clause'
Thanks in advance
Zend_Db_Select tries to delimit strings as column names, but it skips doing that if you pass an object of type Zend_Db_Expr instead of a string:
->order(new Zend_Db_Expr("name like 'C%' DESC"))->
There's also an undocumented shortcut: the column-delimiting function assumes that any string containing parentheses is very likely to be an expression instead of just a column name. So the following would work too:
->order( "(name like 'C%' DESC)" )->
I'm not familiar with zend but Try to rewrite your sql query in Zend like this
select *, (`name` like 'C%') as theFiled from table_name by theFiled desc;
Though i'm not so sure I guess that the Zend Code must be something like this,
$result = $this->getDefaultAdapter() ->select()
->from($this->_name,array('*', "theFiled" => "name like 'C%'") ->order("theFiled DESC")
->query() ->fetchAll();