[英]SQL query to hide the result of a single cell based on the value of another cell
I am trying to come up with a query where given a table such as below, displays the list of games but hides the answer column for the rows where inProgress = 0.我正在尝试提出一个查询,其中给定如下表,显示游戏列表,但隐藏 inProgress = 0 的行的答案列。
The only way I can think of is doing a simple query such as SELECT * FROM game WHERE inProgress = 0;
我能想到的唯一方法是做一个简单的查询,例如
SELECT * FROM game WHERE inProgress = 0;
is there any other function of SQL that lets me only hide or set the value of the answer column to the desired value where inProgress = 0?是否有任何其他 SQL 函数让我只能隐藏或将答案列的值设置为 inProgress = 0 的所需值?
Thanks!谢谢!
Use a case
.使用
case
。 It's like an if/else
for selecting columns.这就像用于选择列的
if/else
。
select
gameId,
case
when inProgress = 0 then
null
else
answer
end as answer,
inProgress
from game
This will show all rows in game
but if inProgress = 0
it will not show the answer.这将显示
game
中的所有行,但如果inProgress = 0
则不会显示答案。
Take a look at MySql case statement.看一下 MySql 的 case 语句。 You won't be able to necessarily hide the column, but you could put a value such as N/A or -1 in it to display what you are trying to get across.
您不一定能够隐藏该列,但您可以在其中输入 N/A 或 -1 之类的值来显示您想要了解的内容。
SELECT gameId, inProgress, CASE when inProgress = 0 then -1 ELSE answer AS answer FROM game;
A similar but more compact form of a case expression , you could use an inline if一种类似但更紧凑的case 表达式,您可以使用内联 if
select gameId, if(inProgress = 1, null, answer) Answer, InProgress
from game;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.