Concatenate Field with Plain Text in CASE used in Select?
I may end up having to do this with my PHP on the result set, but I was hoping the DB could do this for me. I'm wanting to check a field and if it matches a condition, set another field equal to another field + a string. Here's my attempts:
SELECT CASE UnityReq WHEN '1' T开发者_开发问答HEN Table + ' (Unity)' ELSE Table END AS Type
If the Table were equal to Event, I'd want to get 'Event (Unity)' as Type.
I saw in a different post you have to cast your field if combining with a string, so I tried this:
SELECT CASE WHEN UnityReq = 1 THEN CAST(Table AS NVARCHAR(200)) + ' (Unity)' ELSE Table END AS Type
And tried the MySQL CONCAT function:
SELECT CASE UnityReq WHEN 1 THEN CONCAT(Table, ' (Unity)') ELSE Table END AS Type
I've also tried both WHEN's with or without the quotes around the number (as it's an INT field), and about every format I've been able to find googling for CASE, but I'm always getting SQL syntax errors. I feel like I've tried every version of this command I can come up with but I'm not having any luck. Any pointers?
It's not CASTing you're after, but CONCATenation.
Something like:
SELECT
CASE WHEN UnityReq = 1 THEN CONCAT(Table, ' (Unity)') ELSE Table END AS Type
FROM ...
精彩评论