0

I'm a SQL newbie, in that case, I want to perform an IF..THEN in SQL SELECT statement, I try to write code like this, but it returns an error syntax

Here's my code :

SELECT *,IF(total_skor <= 100 AND total_skor >= 80,"Amat Baik")
AS pred FROM rekap_nilai ORDER BY id ASC

Can you tell me what's wrong in my code? Thanks sir/madam.

2

3 Answers 3

1

You missing else part in your query otherwise your query ok.Below query with else part.

SELECT *,IF(total_skor <= 100 AND total_skor >= 80,"Amat Baik",NULL) AS pred FROM rekap_nilai ORDER BY id ASC;
Sign up to request clarification or add additional context in comments.

Comments

0

The MySQL IF function takes three arguments. The first is evaluated as a boolean expression, if it evaluates to true (any non-zero integer value), the function returns the second argument, otherwise, it returns the third argument.

Your IF is missing a third argument, a value to return when the boolean expression does not evaluate to TRUE.

This can be any expression, including a literal empty string or NULL keyword, whatever you want to return.

    IF(total_skor <= 100 AND total_skor >= 80,'Amat Baik','foo')
                                                         ^^^^^^

NOTES:

Your first argument could also be re-written using a BETWEEN comparison, e.g.

    IF(total_skor BETWEEN 80 AND 100, 'Amat Baik', '')

Comments

0

Use the following IF statement

SELECT *, IF (total_skor <= 100 AND total_skor >= 80,"AmatBaik",NULL)AS pred 
FROM rekap_nilai 
ORDER BY id ASC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.