0

I am trying to figure out a way to do this:

If (blah = 0 then 'Yes' else if blah = 1 then 'no' else 'maybe')

in mysql. I am trying to do this since I inserted all my data into a gridview via:

    Dim strSQL As String
    strSQL = "SELECT id, billing_first_name, billing_last_name, billing_email, billing_address, billing_city, billing_state, billing_zip, " & _
                "total, price_mode, process_status, 1 as num_of_items " & _
             "FROM orders " & _
             "ORDER BY created_on DESC, processed_on DESC LIMIT 0, 20;"

    Dim dtReader As MySqlDataReader
    objCmd = New MySqlCommand(strSQL, objConn)
    dtReader = objCmd.ExecuteReader()

    grdView.DataSource = dtReader
    grdView.DataBind()

If I can do the if/then/else somewhere in vb.net code above instead of in the database then that would be nice as well. I'm more used to doing it that way with normal query code.

I can currently do this with only if and then else, not if, elseif and then else.

SELECT IF(process_status = 1, 'SUCCEEDED', 'blah') as message, id,...

Thanks!

3 Answers 3

1

In SQL, that's done like this:

select 
      case blah when 1 then 'yes' 
                when 0 then 'no' 
      else 'maybe' end as blah ,
      columnb  ,
      columnc
from table
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT field1,field2,field3,
CASE field3
WHEN 0
THEN 'YES'
WHEN 1
THEN 'NO'
ELSE
'MAYBE'
END AS blah
FROM table

Comments

1

A terser alternative to a CASE expression is to nest two IF functions in an expression, e.g.

SELECT If(blah=0,'Yes',IF(blah=1,'no','maybe')) AS blah

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.