Laravel: Unknown column when using alias in DB::raw() - php

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.

Related

Laravel Query Builder weird column names conversion

I am trying to figure out why is DB::raw() queries are getting sent as lowercases?
Query = DB::('table_name')->select(['is_read as isRead'])->get();
Result = [{ isRead: true }]
But when I do raw its converting it to lowercase
Query = DB::('table_name')->select(DB::raw('is_read as isRead'))->get();
Result = [{ isread: true }]
I have a reason to use DB raw so I really need to figure this thing out.
I'm not able to reproduce the issue you mention... Which version of Laravel are you using?
For instance, if I run the following:
DB::table('users')->select(DB::raw('is_read as isRead'))->get();
I would get the error:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'is_read' in 'field list' (SQL: select is_read as isRead from users)'
Which is normal because I have no is_read column in my users table. However, you can see in the error that the isRead is not converted to lowercase.
Maybe you can use selectRaw instead:
DB::table('table_name')->selectRaw('is_read as isRead')->get();
The query would be:
select is_read as isRead from `table_name`
Otherwise, could you update your question to provide more information on how to reproduce so your issue with the capitalization.
I can confirm that when using DB:raw (or orderByRaw or any row raw methods) column names get converted to lowercase.
To avoid that, you can try to protect the name with quotes:
Query = DB::('table_name')->select(['is_read as "isRead"'])->get();

Error Code: 1054 Unknown column 'opdrachten.OpdrachtID' in 'field list'

When I run this query I get an error message but I dont know what Im doing wrong.
SELECT deelnemers.StudentenID, voorkeur.VoorkeurID,
opdrachten.OpdrachtID, voorkeur.Voorkeur
FROM deelnemers
INNER JOIN voorkeur, opdrachten ON voorkeur.StudentenID=deelnemers.StudentenID
WHERE VoorkeurID=1
AND voorkeur.Voorkeur=opdrachten.OpdrachtID
This is query is inserted in an table. Using PHP but when I tried running the query with SQLyog but still the same problem. I also checked multiple times if the column exists and yes it does
SELECT deelnemers.StudentenID, voorkeur.VoorkeurID, opdrachten.OpdrachtID,
voorkeur.Voorkeur FROM deelnemers
INNER JOIN voorkeur ON voorkeur.StudentenID = deelnemers.StudentenID INNER JOIN
opdrachten
ON voorkeur.StudentenID = deelnemers.StudentenID
WHERE VoorkeurID=1 AND voorkeur.Voorkeur = opdrachten.OpdrachtID
This was the what I used and Now I got what I want

Unknown column 'A.VOLUME' in 'field list' WHEN UPDATE A COLUMN

Not sure if this is a php/mysql bug however whenever I try to update a Column through PHP I get the following error 'Unknown column 'tablename.columnname' in 'field list'... just to make sure I typed up different variations of UPDATE queries directly on Terminal to test the syntax and they work fine, when I do the same through PHP the same error message..
CURRENTLY I'm trying to run this following query.
$updateSTKLISTVol = "UPDATE STKLIST AS A INNER JOIN (SELECT SYMBOL, VOLUME FROM $tablename WHERE id ='1') AS B ON A.SYMBOL = B.SYMBOL SET A.VOLUME=B.VOLUME";
$result5 = mysqli_query($dbcon, $updateSTKLISTVol) or die(mysqli_error($dbcon));
the error return is...
"Unknown column 'A.VOLUME' in 'field list' "
I would appreciate any guidance, thank you in advance.

Laravel: Left join sql error

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

OpenCart DB query issue (syntax misunderstand)

OpenCart v1.5.3.1, Export plugin:
$query = "SELECT pd.*, cg.name FROM `".DB_PREFIX."product_discount` pd ";
$query .= "LEFT JOIN `".DB_PREFIX."customer_group` cg ON cg.customer_group_id=pd.customer_group_id ";
$query .= "ORDER BY pd.product_id, cg.name";
Can anybody explain, what pd and cg means here?
Meet similar sintax in other places, but not sure what it means and how to work with it...
Looks like it is some common thing, but I am quite new to work with data bases yet, please help :-(
This query generate error:
Notice: Error: Unknown column 'cg.name' in 'field list' Error No:
1054
pd is the alias name for table `product_discount`
cg is the alias name for table `customer_group`
Notice: Error: Unknown column 'cg.name' in 'field list' Error No: 1054
This would actually mean there is no field of name in the table customer_group

Categories