Create a query by checking two columns

1

I have a following table:

tbl_missao

  • id_misssao
  • hostname
  • status
  • war_id
  • commit_server_id

When you create a mission, it can add that mission to itself or you can tell another user (warrior) to perform this mission. The logic is as follows:

  • When creating a mission without indicating a warrior, it creates a mission record for the user to be registering leaving the id_guerreiro = NULL field.
  • When creating a mission indicating the warrior, create a record in the table containing the warrior's identifier and the mission's author.

Problem

I would like to create a query that shows the total number of quests a user has, but you must create a condition to check whether id_guerreiro is null or not.

SE war_id is equal to NULL, then THEN return a counter checking the quantity of items that exist for id_criador_da_missao
SENÃO return a counter checking the total amount of items of id_guerreiro .

The following query calculates the total of items, but only by checking the id_criador_da_missao column. Is there a way to check the two columns and return the total?

SELECT COUNT (id_criador_da_missao ) TOTAL FROM TBL_MISSAO
    
asked by anonymous 05.12.2016 / 17:52

1 answer

1

The following query will return the total for you, where the @id_usuario variable should be replaced with the user code you are looking for:

SELECT COUNT(1) AS total
  FROM TBL_MISSAO
 WHERE (id_guerreiro IS NULL AND id_criador_da_missao = @id_usuario)
    OR (id_guerreiro = @id_usuario)
    
05.12.2016 / 18:02