你如何在 MySQL select 语句中编写条件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1647961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:23:23 来源:igfitidea点击:
How do you write a conditional in a MySQL select statement?
提问by Langdon
I'm using MySQL, and I want to do a sort of ternary statement in my SQL like:
我正在使用 MySQL,并且我想在我的 SQL 中执行一种三元语句,例如:
SELECT USER_ID, ((USER_ID = 1) ? 1 : 0) AS FIRST_USER
FROM USER
The results would be similar to:
结果将类似于:
USER_ID | FIRST_USER
1 | 1
2 | 0
3 | 0
etc.
How does one accomplish this?
如何做到这一点?
回答by x2.
SELECT USER_ID, (CASE USER_ID WHEN 1 THEN 1 ELSE 0 END) as FIRST_USER FROM USER