mysql查询if else语句?

时间:2010-03-14 20:10:39

标签: sql mysql

我有这个SQL查询:

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

我想确定S.STATUS是1还是0,并根据这些值在查询结果中返回COMPLETEPENDING

2 个答案:

答案 0 :(得分:8)

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

IF functionif(x=y, TRUE RESULT, FALSE RESULT)

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

答案 1 :(得分:1)

您也可以通过以下方式执行此操作 -

    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

如果您有任何问题,请告诉我。