user253530
user253530

Reputation: 2591

mysql query if else statement?

I have this sql query:

SELECT
S.SEARCH,
S.STATUS,
C.TITLE AS CategoryName,
E.SEARCH_ENGINES AS Engine,
S.RESULTS,
S.DATE,
S.TOTAL_RESULTS AS Total,
S.ID

FROM
PLD_SEARCHES AS S
Join PLD_CATEGORY AS C ON C.ID = S.CATEGORY_ID
Join PLD_SEARCH_ENGINES AS E ON S.SEARCH_ENGINES_ID = E.ID
ORDER BY S.DATE ASC

I want to identify if S.STATUS is either 1 or 0 and according to those values to return COMPLETE or PENDING in the query results

Upvotes: 2

Views: 10234

Answers (2)

Anjuman
Anjuman

Reputation: 1444

You can do this by the following way too -

    SELECT
    S.SEARCH,
    CASE WHEN S.STATUS=1 THEN \'COMPLETE\' 
    ELSE \'PENDING\' END AS S.STATUS,
    C.TITLE AS CategoryName,
    E.SEARCH_ENGINES AS Engine,
    S.RESULTS,
    S.DATE,
    S.TOTAL_RESULTS AS Total,
    S.ID

    FROM
    PLD_SEARCHES AS S
    Join PLD_CATEGORY AS C ON C.ID = S.CATEGORY_ID
    Join PLD_SEARCH_ENGINES AS E ON S.SEARCH_ENGINES_ID = E.ID
    ORDER BY S.DATE ASC

Let me know if you have any problem.

Upvotes: 1

Andy
Andy

Reputation: 17771

SELECT S.SEARCH, if(S.STATUS=1,'COMPLETE','PENDING') as STATUS, 
C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine, S.RESULTS, 
S.DATE, S.TOTAL_RESULTS AS Total, S.ID
FROM PLD_SEARCHES AS S 
Join PLD_CATEGORY AS C ON C.ID = S.CATEGORY_ID 
Join PLD_SEARCH_ENGINES AS E ON S.SEARCH_ENGINES_ID = E.ID 
ORDER BY S.DATE ASC

The IF function is if(x=y, TRUE RESULT, FALSE RESULT)

if(S.STATUS=1,'COMPLETE','PENDING')

Upvotes: 8

Related Questions