Why Can't I use or why does it encounters an error. I'm trying to use a script as such as the one below. I'm a newbie when it comes to mysql and I tried creating scripts like this but I get the errr like the below
#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 '1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.statu' at line 4
Here's my script
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount =
CASE WHEN dogdetails.dogcount IS NOT 1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.status = 'Checked'
WHEN dogdetails.dogcount = 1 THEN dogs.pto = pto - dogdetails.total, dogdetails.status = 'Checked'
WHERE dogdetails.id=4
Is there somethng I'm missing or overlooked?
remember that you have to use the keyword 'end' after your case statements and to always return a value. The IS NOT keyword is also incorrect when comparing numbers. Here's a version of your query that should work:
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount = CASE WHEN dogdetails.dogcount != 1 THEN dogs.dogcount = dogcount-dogdetails.total else dogs.dogcount end,
dogs.pto= case WHEN dogdetails.dogcount = 1 THEN pto - dogdetails.total else pto end,
dogdetails.status = 'Checked'
WHERE dogdetails.id=4
Related
UPDATE table_one
SET user_accessible = 1
WHERE volume == 2
AND lesson_order == '04'
LIMIT 1
It's giving the following error:
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 '= '2' AND lesson_order == '04') LIMIT 1' at line 1
The query looks correct to me :(
You made a mistake on your comparison. Use single = not ==
UPDATE table_one
SET user_accessible = 1
WHERE volume = 2
AND lesson_order = '04'
LIMIT 1
I have to "port" an SQLite database to MySQL. The database is set up from a C# application with the Connector/NET lib.
Porting the methods for creating the tables of the DB was straight forward, but I got stuck at a view.
The SQLite code for the view works just fine and looks like the following:
CREATE VIEW IF NOT EXISTS EXO_ResultsSummaries AS
SELECT ParticipantBIB,
t.HeatIndex,
(CASE WHEN t.QualiGroup IS NULL THEN t.Finals ELSE t.QualiGroup END) AS HeatLevel,
PointsObstacle0 + PointsObstacle1 AS TotalScore, TimeObstacle0 + TimeObstacle1 AS TotalTime
FROM (EXO_Results INNER JOIN Heats ON EXO_Results.HeatIndex = Heats.HeatIndex) t
WHERE t.Completed = 1;
Since the "IF NOT EXISTS" statement does not work with MySQL, I changed the code to the following:
CREATE OR REPLACE VIEW EXO_ResultsSummaries AS
SELECT ParticipantBIB,
t.HeatIndex,
(CASE WHEN t.QualiGroup IS NULL THEN t.Finals ELSE t.QualiGroup END) AS HeatLevel,
PointsObstacle0 + PointsObstacle1 AS TotalScore, TimeObstacle0 + TimeObstacle1 AS TotalTime
FROM (EXO_Results INNER JOIN Heats ON EXO_Results.HeatIndex = Heats.HeatIndex) AS t
WHERE t.Completed = 1;
However, if I try to create the view from my application (or from the workbench), I get this error:
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 'AS t WHERE t.Completed = 1' at line 6
Thanks to Shadow for his comments. Changed my code accordingly and this worked just fine:
CREATE OR REPLACE VIEW EXO_ResultsSummaries AS SELECT ParticipantBIB,
Heats.HeatIndex,
(CASE WHEN Heats.QualiGroup IS NULL THEN Heats.Finals ELSE Heats.QualiGroup END) AS HeatLevel,
PointsObstacle0 + PointsObstacle1 AS TotalScore, TimeObstacle0 + TimeObstacle1 AS TotalTime
FROM EXO_Results INNER JOIN Heats ON EXO_Results.HeatIndex = Heats.HeatIndex
WHERE Heats.Completed = 1;
here's the generated query:
UPDATE namelist
SET 'submitterName' = 'Jim'
,'actorName' = 'dingle'
,'setYear' = '1103'
,'country' = 'tanata'
,'blink' = 'on'
,'crush' = 'on'
,'initialize' = 'on'
,'entered' = 'on'
,'stuck' = 'on'
,'catapult' = 'on'
,'ruck' = 'on'
WHERE id = 31
it generates this (less than helpful) error:
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 (query snippet) at line 1
for the life of me, i can't spot where the problem is. yes, column names match, yes
TIA for helping out.
WR!
You have used single quotes before and after columns in the query, replace those single quotes with backquotes.
So the query like
UPDATE namelist
SET `submitterName`='Jim',
`actorName`='dingle',
`setYear`='1103',
`country`='tanata',
`blink`='on',
`crush`='on',
`initialize`='on',
`entered`='on',
`stuck`='on',
`catapult`='on',
`ruck`='on'
WHERE id=31;
user ` instead of '
like this
UPDATE namelist SET `submitterName`='Jim',`actorName`='dingle',`setYear`='1103',`country`='tanata',`blink`='on',`crush`='on',`initialize`='on',`entered`='on',`stuck`='on',`catapult`='on',`ruck`='on' WHERE id=31
Am I blind, or what is wrong with my query?
select
STRCMP( message, 'LogMessage') = 1
from
LogEntries;
works fine. However
select
IF STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF
from
LogEntries;
returns:
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 'STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF from
LogEntries' at line 2
What is wrong with this statement?
You might want to use CASE WHEN:
SELECT
CASE WHEN STRCMP( message, 'LogMessage') = 1 THEN 'bla' END AS your_column
FROM
LogEntries;
when the condition is true it will return 'bla' otherwise, since there's no else part, it will return NULL.
I'm trying to create this trigger and I get this error:
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 'case where artcStackId = new.artcStackId' at line 7
Here is the failing trigger
SET new.artcPublicId =
case
when new.artcCountry = 'US' then concat(91,new.artcStackId)
when new.artcCountry = 'UK' then concat(92,new.artcStackId)
when new.artcCountry = 'CA' then concat(93,new.artcStackId)
else concat(11,new.artcStackId)
end case
where artcStackId = new.artcStackId
Here is the trigger that works
SET new.artcPublicId =
case
when new.artcCountry = 'US' then concat(91,new.artcStackId)
when new.artcCountry = 'UK' then concat(92,new.artcStackId)
when new.artcCountry = 'CA' then concat(93,new.artcStackId)
else concat(11,new.artcStackId)
end
when I add those extra bits, it'll fail.
There is no reference to your where condition.i think it should be like where new.artcStackId = new.artcStackId
Your first trigger has a syntax error:
end case
should be just
end
ie remove the trailing "case"
Edit:
I just noticed you have a where condition: where artcStackId = new.artcStackId.
You can't do that. You can only modify the current row, referenced by the new. syntax.
You must remove the where clause altogether, because you may only update the row being inserted/updated.