Create a query by checking two columns

Asked

Viewed 38 times

1

I have a following table:

tbl_mission

  • id_misssao
  • assignment
  • status
  • id_warrior
  • id_creator

When the user creates a mission, he can add this mission to himself or he can indicate another user (warrior) to accomplish this mission. The logic is as follows:

  • When creating a mission without indicating a warrior, creates a mission record for the user himself who is registering leaving the field id_guerreiro = NULL.
  • When creating a mission indicating the warrior, create a record in the table containing the identifier of the warrior and the author of the mission.

Problem

I would like to create a query that shows the total number of missions a user has, but it is necessary to create a condition to verify that the id_guerreiro is void or not.

IF id_warrior equals NULL,
THEN returns a counter by checking the amount of items that exists for the id_criador_da_missao
OTHERWISE return a counter by checking the total amount of items in the id_guerreiro.

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

SELECT COUNT (id_criador_da_missao ) TOTAL FROM TBL_MISSAO
  • You want to return all in the same counter or you want to return an accountant to missions that belong to this creator and missions of which he is the warrior?

  • @Sorack I want to return the total amount of missions that a warrior has, but when there is no warrior in the item, you have to check the identifier of the creator of the mission that would be the mission director himself.

  • Okay, so the answer I put in will do for you

1 answer

2


The following query will return the total for you, where the variable @id_usuario should be replaced by 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)
  • 1

    It really worked this way. I’ll do some more tests and validate your reply. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.