我的表名为chat_users
,其中包含列:
以下是使用CASE语句的UPDATE示例:
$db->exec("UPDATE `proba13`.`chat_users`
SET `username` = CASE id
WHEN 4 THEN 'Jie'
WHEN 5 THEN 'Mie'
WHEN 6 THEN 'Pres'
END,
`status` = CASE id
WHEN 4 THEN '1'
WHEN 5 THEN '2'
WHEN 6 THEN '3'
END
WHERE id IN (4,5,6)");
问题: 任何人都可以在同一张桌子上给我一个SELECT with CASE语句的例子吗?
问候和 提前谢谢!
答案 0 :(得分:1)
这将返回与更新的表相同的结果(但是,显然没有对其进行任何更改):
SELECT id,
CASE id
WHEN 4 THEN 'Jie'
WHEN 5 THEN 'Mie'
WHEN 6 THEN 'Pres'
ELSE username
END AS username,
CASE id
WHEN 4 THEN 1
WHEN 5 THEN 2
WHEN 6 THEN 3
ELSE status
END AS status,
time_mod,
views
FROM proba13.chat_users
答案 1 :(得分:0)
试试这个
SELECT (CASE WHEN
id =4 THEN 'Jie'
WHEN id =5 THEN 'Mie'
WHEN id =6 THEN 'Pres'
END ) `username` ,
(CASE WHEN
id= 4 THEN '1'
WHEN id= 5 THEN '2'
WHEN id= 6 THEN '3'
END ) `status` FROM `chat_users`
WHERE id IN (4,5,6)