I am trying to use the CASE statement, but I keep getting a syntax error. I have followed the docs but I cannot seem to get rid of this syntax error:
query:
SELECT students.firstname, students.lastname, students.tnumber, grades.tnumber
FROM students, grades
CASE WHEN grades.tnumber=students.tnumber AND grades.courseid='CSC2110' AND (grades.Grade='A' OR grades.Grade='B' OR grades.Grade='C')THEN 'YES' ELSE 'NO' END
error:
error executing query. MySQL: 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 WHEN grades.tnumber=students.tnumber AND grades.courseid='CSC2110' A' at line 1
Ignore the tables and select statements. I am more concerned are checking the syntax of my CASE statement and why it is throwing an error.
It isn't completely clear what you want the CASE expression to do, but it appears that it should be in the SELECT list. I also moved the join condition, which strangely appeared in the CASE expression, to the ON clause.
SELECT students.firstname,
students.lastname,
students.tnumber,
grades.tnumber,
CASE WHEN grades.courseid = 'CSC2110' AND
(grades.Grade='A' OR grades.Grade='B' OR grades.Grade='C')
THEN 'YES' ELSE 'NO' END AS someColumn
FROM students
INNER JOIN grades
ON grades.tnumber = students.tnumber
You can also try this one mate, using proper and readable syntax:
SELECT
s.tnumber, s.firstname, s.lastname,
IF (
-- condition
grades.Grade = 'A' OR grades.Grade = 'B' OR grades.Grade = 'C',
-- if true
'YES',
-- else
'NO'
) `status`
FROM
students s
INNER JOIN grades g ON g.tnumber = s.tnumber
WHERE grades.courseid = 'CSC2110';
Note
You can where that I have included a WHERE section where you can easily reuse this code with other courseid
I also added a suggested format when presenting data which is for people, always put the id code infront
One more point is that, by using a proper format, it help you and others to understand your code
Related
I'm trying to build an SQL-Query with MySQL-Version 5.7.27.
I'm trying to make multiple left joins to the same table. At some point i get an error, that tells me, a column of the original table is unknown.
I tried finding the syntax error in my query, but there can't seem to be an obvious one. I tried out some online SQL-validators. They keep telling me, my query is valid.
After that i tried picking apart the query, to find out, what exactly i am working with before the left join that makes my query unable to function. Doing that actually showed me a result, that was including the column the error was telling me, that is missing.
So maybe it is not a syntax error, but MySQL is validating things in a order different to what i know, trying to pick the "missing" column out of somewhere it actually doesn't exist.
Changing the order of left joins actually solves the problem. I can't really do that, tho. The SQL is being generated by some functions. One for every part of the SQL. One for the "SELECT", one for the "FROM" and so on. So a working thing is joining the minimized versions of the "additional_agreement_fields" table before the other joins. Sadly the function for the "FROM" part is written ... in a very interesting way. So my best bet is adding the "additional_agreement_fields" joins after the function that creates the FROM.
That attempt generated the SQL below.
I get following error:
Error Code: 1054. Unknown column 'customers.id' in 'on clause'
So:
- The parts of the query are working on their own
- Execution until the point of the error holds a table that has the
"missing column"
- Changing the order of the joins solves the problem, but i can't just
do that and i want to know, why it doesn't work in this order
- Online-validators can't find any syntax errors
- Logically i am just adding columns to a table that are not ambiguous
and not removing any. Yet something becomes unable to be found
SELECT DISTINCT
(customers.id),
companies.company_name AS 'Kunde -> Firmenname',
vcpt_mail.tcom_value AS 'Kontakt -> TCom -> Email',
v_contact_people.id AS 'Kontakt -> ID',
v_contact_people.last_name AS 'Kontakt -> Nachname',
A0.value AS 'Kunde -> Zusatzvereinbarung -> TestfeldB'
FROM
customers
LEFT JOIN
customer_types ON customer_types.id =
customers.customer_type_id,
v_customer_owners,
companies
LEFT JOIN
(v_contact_people
LEFT JOIN v_cp_tcoms AS vcpt_mail ON
vcpt_mail.contact_person_id = v_contact_people.id
AND vcpt_mail.ttid = 3
AND (vcpt_mail.type_label_access_path = '/Global'
OR vcpt_mail.type_label_access_path LIKE '/Global%')) ON
v_contact_people.company_id = companies.id
AND v_contact_people.access_path LIKE '/Global%'
LEFT JOIN
(SELECT
*
FROM
additional_agreement_fields
WHERE
name = 'TestfeldB' AND value = '5') AS A0 ON customers.id
= A0.customer_id
LEFT JOIN
(SELECT
*
FROM
additional_agreement_fields
WHERE
name = 'TestfeldC' AND value = '6') AS A1 ON customers.id
= A1.customer_id
WHERE
v_customer_owners.customer_id = customers.id
AND v_customer_owners.path LIKE '/Global%'
AND customers.company_id = companies.id
AND companies.deleted_at IS NULL
AND customers.deleted_at IS NULL
AND (A0.value = '5' AND A0.name = 'TestfeldB'
OR A1.value = '6' AND A1.name = 'TestfeldC');
The expected result is a not empty table with 2 customers in it, i prepared to find and that are found, changing the join order.
The result is an interrupted query with an error code.
Error Code: 1054. Unknown column 'customers.id' in 'on clause'
This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#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 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.
I am getting error in the partition clause. Please help me. The error is
Select Model_Name,
Case Model_Subtype
When 'Weight' then 'Static'
When 'Linked Account' then 'Dynamic'
When 'Flexible' then 'Dynamic'
Else 'Not Defined'
End as Model_Type, Security_Name, Market_Value,Weight,
Case When Weight = 0 And Market_Value= 0 Then 0
When Weight = 0 Then Cast(Market_Value/ nullif(SUM(market_Value)
OVER (Partition by Model_Name),0) AS Decimal (10,4))
When Weight !=0 Then Weight/100
Else Weight End as Target_Weight,
vehicle.Vehicle_Name
from
OFF_PRODUCT_MODEL model
Join OFF_PRODUCT_MODEL_TARGET target on target.Model_Id = model.Model_Id
Join OFF_PRODUCT_SECURITIES Sec on sec.Security_Id = target.Security_Id
left outer Join Offer_Back_End.tblStrategies Strategy on Strategy.Vestmark_Sleeve_Code = model.Model_Name
left outer join Offer_Back_End.tblVehicles vehicle On vehicle.Vehicle_Id = strategy.Vehicle_ID
Where (Strategy.Active_Strategy is null or Strategy.Active_Strategy = 1)
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 '(Partition by Model_Name),0) AS Decimal (10,4))
When Weight !=0 Then Weight/' at line 11
Umm, OVER (Partition by Model_Name) is analytical function which MySQL unfortunately doesn't support and don't have them and so you are getting the said error.
MySQL does not support OVER() clause. In general Window Functions are not part of MySQL.
You can however emulate it - please refer to the following answer:
https://stackoverflow.com/a/20432371/900564
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem.
ERROR: Unclosed quote # 173
STR: "
update voucher_nos
set (select voucher_type as points from vouchers where id='1') = points+1
where company_id = '24'
and finance_year='01/01/2014-01/01/2015';
update voucher_nos
set (select voucher_type as points from vouchers where id='1') = points+1
where company_id = '24'
and finance_year='01/01/2014-01/01/2015'";
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 '(select voucher_type as points from vouchers
where id='1') = points+1 w' at line 1
helps will be appreciated
and finance_year='01/01/2014-01/01/2015'";
You have an unneccesary " at the end here, which is probably causing the Syntax error.
--
The second problem is that the first part of a SET statement should be a column name, not a value (or a query which returns one). But I'm not sure how to fix that unless you explain a bit more what you're trying to accomplish.
I have (3) tables: Professor, Comment, Course
I'm trying to get a few columns to display the professor with all the courses and all the comments they have.
My tables
SQL:
SELECT prefix, code, info, date
FROM Course, Comment
JOIN Professor ON Professor.pID = Comment.pID
AND JOIN Course ON Course.cID = Comment.cID
WHERE Comment.pID = ?
Throws error:
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 'JOIN Course ON Course.cID = Comment.cID WHERE Comment.pID = '273'' at line 4
SELECT prefix, code, info, date
FROM Comment
JOIN Professor ON Professor.pID = Comment.pID
JOIN Course ON Course.cID = Comment.cID
WHERE Comment.pID = ?
Just remove the AND before the second JOIN and remove Course in the From list. You may need to change the order of the tables depending on what table data you are primarily after.
Advise that you have a read through the MySQL JOIN syntax page.
change 'AND JOIN' to just 'JOIN'
There is no AND JOIN. Please read about JOIN syntax in the manual before attempting to use it.
Additionally, you reference table Course twice.