我正面临SQL Server的SQL查询问题。
我有一个像这样的查询。
SELECT QUOTENAME(A, '"'),
QUOTENAME(B, '"'),
QUOTENAME(C, '"')
FROM TABLENAME
现在,我必须用CASE语句替换它。
现在这个查询有效 -
SELECT A,
CASE
WHEN B='Thank you' then 'TY'
WHEN B='No Problem' then 'NP'
WHEN B='As soon as possible' then 'ASAP'
END AS B,
C
FROM TABLENAME
现在我正在使用QUOTENAME,因为我想用双引号括起字段。任何可能的场景我如何混合QUOTENAME和CASE语句。无论我尝试过什么都会引发语法错误。
答案 0 :(得分:2)
应该像这样工作
SELECT A,
CASE
WHEN B='Thank you' then QUOTENAME('TY', '"')
WHEN B='No Problem' then QUOTENAME('NP', '"')
WHEN B='As soon as possible' then QUOTENAME('ASAP', '"')
END AS B,
C
FROM TABLENAME
或更短
SELECT A,
QUOTENAME( CASE
WHEN B='Thank you' then 'TY'
WHEN B='No Problem' then 'NP'
WHEN B='As soon as possible' then 'ASAP'
END, '"') AS B,
C
FROM TABLENAME
答案 1 :(得分:0)
与GuidoG解决方案相比,这可能会稍微缩短一点:
$pre_check = "select location from users where username='$username";
$result_pre_check = mysqli_query($conn, $pre_check);
$pre_remove = "delete from $result_pre_check where username='$username'";
mysqli_query($conn, $pre_remove);
如果您总是将单个列进行相等性比较,则只需编写SELECT A
, QUOTENAME(CASE B
WHEN 'Thank you' THEN 'TY'
WHEN 'No Problem' THEN 'NP'
WHEN 'As soon as possible' THEN 'ASAP'
END, '"') AS B
, C
FROM TABLENAME;
然后进行比较:CASE COLUMN_NAME
等。