繁体   English   中英

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

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

我正在尝试提出一个查询,其中给定如下表,显示游戏列表,但隐藏 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 的 case 语句。 您不一定能够隐藏该列,但您可以在其中输入 N/A 或 -1 之类的值来显示您想要了解的内容。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM