richard parker
richard parker

Reputation: 11

IF, ELSE statement for sql query

How to write a SQL query where I have a table called ACC_REQUEST?

There is a column in the table called REQUEST_STATUS with the values of 'pending', 'withdrawn', and 'completed

If the request status value = 'pending then select approver_name from ACC_APPROVAL, else select request status

Upvotes: 1

Views: 68

Answers (2)

DWright
DWright

Reputation: 9500

Something like

SELECT 
    CASE
        WHEN ACC_REQUEST.REQUEST_STATUS = 'pending' THEN ACC_APPROVAL.APPROVER_NAME
        ELSE ACC_REQUEST.REQUEST_STATUS
    END as [put the name you wish for output column here, no square brackets]
FROM ACC_REQUEST
INNER JOIN ACC_APPROVAL ON [I'm assuming some kind of join condition here]

Upvotes: 1

dudeman
dudeman

Reputation: 603

You can use CASE

Like:

CASE x WHEN bah THEN FOO END CASE

http://dev.mysql.com/doc/refman/5.0/en/case.html

Upvotes: 1

Related Questions