SQL 查询根据另一个单元格的值隐藏单个单元格的结果

SQL query to hide the result of a single cell based on the value of another cell

我想提出一个查询,其中给定 table 如下所示,显示游戏列表但隐藏 inProgress = 0 的行的答案列。

我能想到的唯一方法是做一个简单的查询,例如 SELECT * FROM game WHERE inProgress = 0; SQL 是否有任何其他功能可以让我只隐藏或将答案列的值设置为期望值,其中 inProgress = 0?

谢谢!

使用 case。它就像一个用于选择列的 if/else

select
  gameId,
  case
  when inProgress = 0 then
    null
  else
    answer
  end as answer,
  inProgress
from game

这将显示 game 中的所有行,但如果 inProgress = 0 则不会显示答案。

看看 MySql 案例陈述。您不一定能够隐藏该列,但您可以在其中放置一个值,例如 N/A 或 -1 以显示您要显示的内容。

SELECT gameId, inProgress, CASE when inProgress = 0 then -1 ELSE answer AS answer FROM game;

一种类似但更紧凑的 case 表达式,您可以使用 inline if

select gameId, if(inProgress = 1, null, answer) Answer, InProgress
from game;