Query using WHERE

Asked

Viewed 58 times

-1

I need to make a query in a table called controls:

inserir a descrição da imagem aqui

I have the spine CH which is the workload in minutes and I have the column SETOR which are the sectors that have participated in some training.

I need to make a query where brings me the total (sum) of minutes per sector. Sector 7 would be for example 1080 the result.

I did so: SELECT round(SUM(ch)) FROM controles WHERE setor = '7';

but it brings me no result.

  • What kind of your field setor?

  • First you have to separate the values of the sector in order to become independent. That is, where it is possible to do a search for ','. For example, the array looks like this: 2,3,4,5. Then I think it will be the "LIKE" and not the = to be used... may be little help, but help in thinking...

1 answer

2


If you want to find all the records where the field setor has certain number you would have to do so:

Note: Understanding that your field is Varchar or some other equivalent to the type of text (String).

SELECT round(SUM(ch)) FROM controles WHERE setor like '%"7"%';

Explaining

Example 1

Select * from Tabela where nome LIKE ‘a%’;

The above character %' indicates that we are seeking all names that start with the letter '.

Example 2

Select * from Tabela where nome LIKE ‘%a’;

The above character %' indicates that we are searching all the names that have the last letter.

Example 3

Select * from Tabela where nome LIKE ‘%a%’;

The two above characters' index indicates that we are searching all the names that have the letter ' a' in any part of the text.

  • The data reported by AP are like this ["2","7","17","97","87"], your like %7% would not get the wrong data no ?

  • @Pedroaugusto I forgot the quotation marks, I modified the answer already.

  • Matheus Ribeiro, thank you very much man. Perfect. With your example using the LIKE I can already do my queries. Very good.

  • 1

    @Richizers accept the answer then. Since our friend helped you

  • @Pedroaugusto, I’m new here. How do I accept the answer? I can do this?

  • Below the arrows of "upvote" and "downvote" should appear a V that can be marked as green. Click on it, simple :)

  • @Pedroaugusto thank you very much

Show 2 more comments

Browser other questions tagged

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