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.
The only way I can think of is doing a simple query such as 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?
Thanks!
CodePudding user response:
Use a case
. It's like an if/else
for selecting columns.
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.
CodePudding user response:
Take a look at MySql case statement. 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.
SELECT gameId, inProgress, CASE when inProgress = 0 then -1 ELSE answer AS answer FROM game;
CodePudding user response:
A similar but more compact form of a case expression, you could use an inline if
select gameId, if(inProgress = 1, null, answer) Answer, InProgress
from game;