2012-08-13 16 views
0

我試圖想出一個辦法做到這一點:的MySQL如果別人對asp.net的GridView

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

如果我能做到的if/then /在vb.net代碼上面,而不是在數據庫中的其他地方,那麼這將是也很好。我更習慣於用普通的查詢代碼這樣做。

我目前可以只用if和else來做這個,而不是if,elseif然後else。

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

謝謝!

回答

1

在SQL中,這樣做了這樣的:

select 
     case blah when 1 then 'yes' 
       when 0 then 'no' 
     else 'maybe' end as blah , 
     columnb , 
     columnc 
from table 
+0

這樣做了,lcarus!謝謝! – StealthRT 2012-08-13 19:34:34

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

感謝帖子,格拉! – StealthRT 2012-08-13 19:35:08

+0

歡迎您StealthRT – Guerra 2012-08-13 19:38:22

1

甲更簡潔替代CASE表達式是巢2 IF在表達功能,例如

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

感謝您的回覆,斯賓塞! – StealthRT 2012-08-13 19:35:28