BowlerMaster::selectRaw("
tour_id, bowler_id, bowler_name, ball_team,
SUM(balls) AS balls,
SUM(overs) AS overs,
DB::raw('SELECT runs, wicket FROM bowler_master WHERE tour_id = ".$request->tour_id." AND bowler_id = ".$request->player_id." ORDER BY wicket DESC, runs LIMIT 1 AS bbi')
")
->where($where_array)
->groupby(['tour_id','bowler_id'])
->get()->first();
In the above Eloquent Query I want to use raw query or any other wayout for getting the result of the raw query to get the value of bbi, but I am getting the following error :
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '::raw('SELECT match_id, runs, wicket FROM bowler_master WHERE tour_id = 1 AND bo' at line 25.
I am writing writing this query for generating following output,
{
"status": 200,
"message": "Success",
"bowler": {
"tour_id": 1,
"bowler_id": 21,
"bowler_name": "Kaushal Chauhan",
"ball_team": null,
"balls": "29",
"overs": 4.5,
"bbi": 9/3,
}
}
Uptil overs calculation, everything is working fine. I am not getting how to get the "bbi" key:value
Note: The result of the query will be pushed in response array and will be converted into json to get the output shown above.
Please guide.
Try this
BowlerMaster::selectRaw("tour_id, bowler_id, bowler_name, ball_team, SUM(balls) AS balls,SUM(overs) AS overs")
->selectSub("
SELECT runs
FROM bowler_master
WHERE tour_id = ".$request->tour_id."
AND bowler_id = ".$request->player_id."
ORDER BY wicket DESC, runs LIMIT 1
","runs")
->selectSub("
SELECT wicket
FROM bowler_master
WHERE tour_id = ".$request->tour_id."
AND bowler_id = ".$request->player_id."
ORDER BY wicket DESC, runs LIMIT 1
","wicket")
->where($where_array)
->groupby(['tour_id','bowler_id'])
->get()->first();
The rules of writing select sub query in laravel is as follows:
DB::table('tablename')
->select('column name','..')
->selectSub("Your full query","alias name")
->get()
I'm trying to use the following code to update columns:
UPDATE user_profiles
SET range = '20', colResize = 'flex'
WHERE uid='472';
I get the following error:
Failed to execute SQL : SQL UPDATE user_profiles SET range = '20', colResize = 'flex' WHERE uid='472'; failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range = '20', colResize = 'flex' WHERE uid='472'' at line 1
What am I overlooking?
range is a reserved keyword in MySQL. Use backticks to escape the name or use another column name.
SET `range` = 20
I'm using NodeJS v8.7.0 with the promise-mysql module, alongside mysql v14.14 Distrib 5.5.57.
I um unable to insert into the temporary table I create. On online SQL validators, as well as my project, I receive the error around the INSERT command.
Whilst I can personally see no issues, I'd love to see a fresh perspective of things to spot the issue.
My whole query string:
CREATE TEMPORARY TABLE QueryBuilder LIKE ItemStore;INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1, 1), (3427, -1, 1, 1), (3428, -1, 1, 1);UPDATE ItemStore AS I JOIN QueryBuilder AS Q ON I.AssetID = Q.AssetID SET I.Owner = Q.Owner, I.GameMode = Q.GameMode, I.GameID = Q.GameID;DROP TEMPORARY TABLE QueryBuilder;
Query line-by-line:
CREATE TEMPORARY TABLE QueryBuilder LIKE ItemStore;
INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1, 1), (3427, -1, 1, 1), (3428, -1, 1, 1);
UPDATE ItemStore AS I JOIN QueryBuilder AS Q ON I.AssetID = Q.AssetID SET I.Owner = Q.Owner, I.GameMode = Q.GameMode, I.GameID = Q.GameID;
DROP TEMPORARY TABLE QueryBuilder;
Extra info:
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1,\' at line 1'
errno: 1064
An SQLFiddle demonstrating why I made this query
Thanks for reading my issue, I appreciate all feedback!
This is query I am using to insert into the table.
I am getting the error like this
$sql = mysql_query("INSERT INTO userinfo(runnername,runnerdob,runnerage,runnergender,runneraddress,runnercity,runnerstate,runnerpincode,runneremail,runnerpassword,runnermobilenumber,e_name,e_relationship,e_address,e_mobilenumber,height,weight,bloodgroup,category,tshirtsize,accountcreationdate,lastlogin,timestamp,registereduser,ipaddress,status)
VALUES('$runnername','$dt',$runnerage,'$runnergender','$runneraddress','$runnercity','$runnerstate','$runnerpincode','$runneremail','$epassword','$runnermobilenumber','$e_name','$e_relationship','$e_address','$e_mobilenumber','$height','$weight','$bloodgroup','$category',$tshirtsize,'$accountdate','Y','$t','Y','$ip',1") or die(mysql_error());
This is generated output
INSERT INTO
userinfo(runnername,runnerdob,runnerage,runnergender,runneraddress,runnercity,runnerstate,runnerpincode,runneremail,runnerpassword,runnermobilenumber,e_name,e_relationship,e_address,e_mobilenumber,height,weight,bloodgroup,category,tshirtsize,accountcreationdate,lastlogin,timestamp,registereduser,ipaddress,status)
VALUES('VIDHYA PRAKASH R','1985-04-08',29,'M','12 DIVINE
RESIDENNCY','coimbatore','TAMILNADU','641035','vidhyaprakash85#gmail.com','FU4A31/GhcmRItAHb97lNtrjRZr+y1yG4arxawG/qEs=','9944524864','rajendran','father','12
DIVINE RESIDENNCY coimbatore TAMILNADU
641035','9894773083','6','6','A1+ve','M',42,'11-07-2014
12:11:02','Y','1405060862','Y','127.0.0.1',1)
but I am getting error as
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1
I am breaking my head for past two hours.
Any one please help me ?
The issue was at the end of the query, writing long insert queries on single line should be avoided and better to write in multiple lines which helps to find the issue almost immediately , or the best is to use prepared statement with PDO or mysqli.
Your query ends as 'Y','$ip',1") which was missing )
Here how the query should be
$sql = mysql_query(
"INSERT INTO userinfo
(
runnername,
runnerdob,
runnerage,
runnergender,
runneraddress,
runnercity,
runnerstate,
runnerpincode,
runneremail,
runnerpassword,
runnermobilenumber,
e_name,
e_relationship,
e_address,
e_mobilenumber,
height,
weight,
bloodgroup,
category,
tshirtsize,
accountcreationdate,
lastlogin,
timestamp,
registereduser,
ipaddress,
status
)
VALUES
(
'$runnername',
'$dt',
$runnerage,
'$runnergender',
'$runneraddress',
'$runnercity',
'$runnerstate',
'$runnerpincode',
'$runneremail',
'$epassword',
'$runnermobilenumber',
'$e_name',
'$e_relationship',
'$e_address',
'$e_mobilenumber',
'$height',
'$weight',
'$bloodgroup',
'$category',
$tshirtsize,
'$accountdate',
'Y',
'$t',
'Y',
'$ip',
1
)"
) or die(mysql_error());
Are you missing a close bracket at the end of your values statement?
Instead of
'$ip',1")
Shouldn't it be
'$ip',1)")
What could be wrong with this line:
'$query = "SELECT * FROM messages WHERE (rlat => '".$latmin."' AND rlat <= '".$latmax."') AND (rlon >= '".$lonmin."' AND rlon <= '".$lonmax."')";'
Error: Error in query: SELECT * FROM messages WHERE (r_lat =>
'55.4655951769' AND r_lat <= '55.496987423') AND (r_lon >=
'25.5338700398' AND r_lon <= '25.5989507602'). You have an error in
your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near '=> '55.4655951769'
AND r_lat <= '55.496987423') AND (r_lon >= '25.5338700398' AN' at line
1...
Thanks!
=> isn't a known operator. If you want the greater than or equal comparison operator, then you are after >=.
Incidentally, you might find that your query can be written more concisely with the BETWEEN ... AND ... operator:
SELECT *
FROM messages
WHERE rlat BETWEEN $latmin AND $latmax
AND rlon BETWEEN $lonmin AND $lonmax
You should also investigate passing variables into your SQL by way of parameters to prepared statements.