I have a query that looks like this that executes and works perfectly:
UPDATE users
SET player_skill = (
(
SELECT
sum(avg_score)
FROM
skill
WHERE
username = "JohnDoe"
AND game = "Bingo"
) -
(
SELECT
sum(avg_score)
FROM
events
WHERE
game = "Bingo"
)
) WHERE username = "JohnDoe"
I want to add a division operator like this:
UPDATE users
SET player_skill = (
(
(
SELECT
sum(avg_score)
FROM
skill
WHERE
username = "JohnDoe"
AND game = "Bingo"
) - (
SELECT
sum(avg_score)
FROM
events
WHERE
game = "Bingo"
)
) / SELECT
COUNT(*)
FROM
skill
WHERE
username = "JohnDoe"
AND game = "Bingo"
)
WHERE
username = "JohnDoe"
It spits out an error code at me
[Err] 1064 - 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 'COUNT(*)
FROM
skill
WHERE
username = "JohnDoe"
AND game = "Bingo" )
) W' at line 21
I've tried for awhile but can't seem to figure out why it wont let it run. I have similar queries to this that seem fine but I cannot get this one to work.
Any help would be greatly appreciated.
try this
UPDATE users
SET player_skill = (
SELECT (sum(s.avg_score) - sum(e.avg_score))/COUNT(e.*)
FROM skill s
INNER JOIN events e on s.game = e.game
WHERE e.username = "JohnDoe"
AND e.username = "Bingo"
)
when you try this query you will get this error :
[Err] 1064 - You have no error in your SQL syntax. you dont need to check the manual that corresponds to your MySQL server version for the right syntax to use no near anything .
Related
I am not that good at SQL but i do as much as i can for the little knowledge i have..
I have made a single a flat SQL string with the help from a friend that gathers data from a table using a relative table from an initial data from the first table, the SQL was made like this:
SELECT id, username, auth_assignment.created_at
FROM `user` JOIN `auth_assignment`
ON (user.id = auth_assignment.user_id)
JOIN `auth_item`
ON (auth_item.name = auth_assignment.item_name)
WHERE auth_item.name = 'Admin'
the initial data to look is Admin so everything works in that side, but i tried to simulate this SQL using Yii2 functions.. so far i have made this code
$query = new Query;
$command = $query->select('id, username')
->innerJoin('auth_assignment', ['user.id'=>'auth_assignment.user_id'])
->innerJoin('auth_item', ['auth_item.name'=>'auth_assignment.item_name'])
->where('auth_item.name = :name', [':name'=>$name])->createCommand();
var_dump($command->query());
this returns an SQLSTATE error:
SQLSTATE[42000]: Syntax error or access violation: 1064 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 'INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN' at line 1
The SQL being executed was: SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`='auth_assignment.user_id' INNER JOIN `auth_item` ON `auth_item`.`name`='auth_assignment.item_name' WHERE auth_item.name = 'Admin'
i checked the method $command->sql; to know how the SQL was being generated.. but i really don't know how to fix it due to my lack of my knowledge to SQL and lack of understanding to yii2 api documentation
SQL is generated like this:
SELECT `id`, `username` INNER JOIN `auth_assignment` ON `user`.`id`=:qp1 INNER JOIN `auth_item` ON `auth_item`.`name`=:qp2 WHERE auth_item.name = :name
i appreciate any help
Try This Query
$query = (new yii\db\Query())
->select('id, username, auth_assignment.created_at')
->from('user')
->innerJoin('auth_assignment','user.id=auth_assignment.user_id')
->innerJoin('auth_item','auth_item.name = auth_assignment.item_name')
->where([
'auth_item.name' => 'Admin'
])->all();
Please let me know why I get an error on the following SQL (I am using a MySQL database) and how to rectify it:
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
AND a.cad_task_completion_date = (SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
WHERE a.ca_id = b.ca_id AND a.ad_id = b.ad_id
)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
I broke the SQL down to ensure each part worked.
SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
And
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
Each of these work on their own. When I combine them I get the following error:
Error
SQL query:
LIMIT 0, 25
MySQL said:
#1064 - 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 'LIMIT 0, 25' at line 1
Regards,
Glyn
EDIT
#Barmar came up with the solution to remove the ";" at the end when testing. However, the result was not what I expected so I ended up changing this to:
SELECT at_award_description.ad_id,
at_award_description.aw_id,
at_award_description.ad_group,
at_award_description.ad_detail,
at_award_description.ad_start_date,
at_award_description.ad_archived_date,
MAX(at_cub_award_date.cad_task_completion_date)
FROM at_award_description
LEFT JOIN at_cub_award_date ON ((at_award_description.ad_id = at_cub_award_date.ad_id)
AND (at_cub_award_date.ca_id = 37))
WHERE at_award_description.aw_id = 5
GROUP BY at_award_description.ad_group
ORDER BY at_award_description.ad_order, at_award_description.ad_group
I'm getting this following error:
MYSQL ERROR
1064 - 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 'DISTINCT R.roomtype as roomtype , I1.meal_plan as
mealplan , I1.`bed_t' at line 3
My Code is
SELECT
DISTINCT H.`hotelname` as `hotelname` ,
DISTINCT R.`roomtype` as `roomtype` ,
I1.`meal_plan` as `mealplan` ,
I1.`bed_type` as `bedtype` ,
I1.`roomrate` as `roomrate` ,
I1.`pax` as `pax` ,
I1.`childrate` as `childrate` ,
I1.`childrate1` as `childrate1` ,
I1.`childrate2` as `childrate2` ,
I1.`childrate3` as `childrate3` ,
P.`profitmarkup_type` as `profit_type` ,
P.`applyprofit_val_room` as `applyprofit_val_room` ,
P.`applyprofit_val_bed` as `applyprofit_val_bed`
FROM `hoteldetails` H
INNER JOIN `roomdetails` R
on H.`hotelname` = R.`hotelname`
INNER JOIN `inventorypolicy` I1
on I1.`hotel_name` = H.`hotelname`
AND I1.`room_plan` = R.`roomtype`
AND I1.`bed_type`=R.`bedtype`
AND I1.`meal_plan`=R.`mealplan`
AND I1.`suppliername`=H.`supplier`
INNER JOIN `profitmarkup` P
on P.`hotel_name` = H.`hotelname`
AND P.`suppliername` = H.`supplier`
WHERE H.`active`='1'
AND H.`country`='MAL'
AND H.`city`='KL'
AND H.`show2web`='1'
AND H.`expiry_date` >= 11/09/2014
AND I1.`inventory_status`='1'
AND P.`markup_status` = '1'
AND 20140911 BETWEEN ((I1.`date1`)+(I1.`month1`*100)+(I1.`year1`*10000)) AND ((I1.`date2`)+(I1.`month2`*100)+(I1.`year2`*10000))
AND 20140911 BETWEEN ((P.`date1`)+(P.`month1`*100)+(P.`year1`*10000)) AND ((P.`date2`)+(P.`month2`*100)+(P.`year2`*10000))
I dont know how to fix this. Any help is greatly appreciated.
As you can learn from the documentation of the SELECT MySQL statement, DISTINCT can be placed before any field or expression to be selected. This automatically forces a single instance of it in a query.
And somewhere down the road there is a sentence that says everything:
DISTINCTROW is a synonym for DISTINCT.
This mysql query runs just fine independently. But when I use this query to create a view then Error Code: 1064 message appears in sql yog. Please identify the reason of this error.
CREATE
VIEW `databaseName`.`viewName`
AS
((SELECT
`tblgrn`.`InitialLabNo`
, `invlabtes`.`Code` AS LabNo
, `invlabmaterial`.`Description` AS material
, `invlabtessubtable`.`SrNo`
, `invlabtessubtable`.`Result`
,COALESCE(NULL, 'Verified') AS TYPE
, `tblgrn`.`Comp_Code`
, `tblgrn`.`Unit_Code`
, `tblgrn`.`UserId`
FROM
`tblgrn`
INNER JOIN `invlabtes`
ON (`tblgrn`.`InitialLabNo` = `invlabtes`.`Code`)
INNER JOIN `invlabtessubtable`
ON (`invlabtes`.`Code` = `invlabtessubtable`.`Code`)
INNER JOIN `invlabmaterial`
ON (`invlabtessubtable`.`TestCode` = `invlabmaterial`.`Code`))
UNION
(SELECT
`tblgrn`.`InitialLabNo`
, `invlabtesscale`.`Code` AS LabNo
, `invlabmaterial`.`Description`
, `invlabscalesubtable`.`SrNo`
, `invlabscalesubtable`.`Result`
,COALESCE(NULL, 'Running') AS TYPE
, `tblgrn`.`Comp_Code`
, `tblgrn`.`Unit_Code`
, `tblgrn`.`UserId`
FROM
`tblgrn`
INNER JOIN `invlabtesscale`
ON (`tblgrn`.`InitialLabNo` = `invlabtesscale`.`IniLabNo`)
INNER JOIN `invlabscalesubtable`
ON (`invlabtesscale`.`Code` = `invlabscalesubtable`.`Code`)
INNER JOIN `invlabmaterial`
ON (`invlabscalesubtable`.`TestCode` = `invlabmaterial`.`Code`)));
this is the error message:
Error Code: 1064
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 'UNION
SELECT
tblgrn.InitialLabNo
, invlabtesscale.Code AS LabNo' at line 22
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.018 sec
Remove the bracket between UNION and SELECT.
UNION
(SELECT
should be
UNION
SELECT
You may need to remove the closing bracket too.
I'm trying to get an explain command to work in mysql. I am trying to troubleshoot the performance of this delete query:
DELETE FROM boost_cache_relationships
WHERE base_dir = 'cache/normal/www.dane101.com'
AND page_callback = 'node'
AND page_type = 'story'
AND page_id = '2891';
This query is the output from a SHOW FULL PROCESSLIST command.
I understand that EXPLAIN does not work with delete so I copied it and replaced DELETE with SELECT to give the following:
explain select
FROM boost_cache_relationships
WHERE base_dir = 'cache/normal/www.dane101.com'
AND page_callback = 'node'
AND page_type = 'story'
AND page_id = '2891';
When I hit enter, mysql gives me an error message that this is invalid SQL:
ERROR 1064 (42000): 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 'FROM boost_cache_relationships WHERE base_dir =
'cache/normal/www.dane101.com' A' at line 1
What am I doing wrong?
You need to specify a field list:
select * FROM boost_cache_relationships WHERE base_dir = 'cache/normal/www.dane101.com' AND page_callback = 'node' AND page_type = 'story' AND page_id = '2891';
A select query needs column names to select. A delete query doesn't.
SELECT *
FROM boost_cache_relationships
WHERE base_dir = 'cache/normal/www.dane101.com'
AND page_callback = 'node'
AND page_type = 'story'
AND page_id = '2891';
In your select try to select some columns:
SELECT *...