0

我试图找出一种方法来做到这一点:

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

在mysql中。我正在尝试这样做,因为我通过以下方式将所有数据插入到 gridview 中:

    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()

如果我可以在上面的 vb.net 代码中的某个地方而不是在数据库中执行 if/then/else,那也很好。我更习惯于使用普通查询代码这样做。

我目前只能使用 if 和 then else 来执行此操作,而不是 if、elseif 和 then else。

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

谢谢!

4

3 回答 3

1

在 SQL 中,这样做是这样的:

select 
      case blah when 1 then 'yes' 
                when 0 then 'no' 
      else 'maybe' end as blah ,
      columnb  ,
      columnc
from table
于 2012-08-13T19:19:52.217 回答
1

CASE 表达式的更简洁的替代方法是在表达式中嵌套两个 IF 函数,例如

SELECT If(blah=0,'Yes',IF(blah=1,'no','maybe')) AS blah
于 2012-08-13T19:34:33.247 回答
1
SELECT field1,field2,field3,
CASE field3
WHEN 0
THEN 'YES'
WHEN 1
THEN 'NO'
ELSE
'MAYBE'
END AS blah
FROM table
于 2012-08-13T19:27:47.817 回答