Mysql query involving Profession and Note information registered in the same column

Asked

Viewed 134 times

2

Consider a table with the following structure: the column contract code corresponds to a foreign key from another table. It is a contract code. Each contract has basically 4 records in this table in question.

The column inf_valor records the types of information: Profession, Address, Name and Note, and each information is recorded separately in this table. In this way, to know which type the information recorded in inf_valor refers to, there is also the column inf_codigo_variable, which also refers to a foreign key from another table.

Through this column (inf_codigo_variable), it is possible to know what kind of information inf_valor refers to. For example, if inf_passcode=117, inf_value refers to the profession. If inf_passcode=124, inf_value refers to the note.

So, I’m looking to mount a query that meets some conditions at the same time, as follows:

Show how many contracts have Note Great by profession Systems Analyst.

In the following way: (inf_variable code) = 124 and (inf_value)=Great and at the same time (inf_code_variable) = 117 e (inf_value)=Systems Analyst

I put the table on http://sqlfiddle.com/#! 9/895e8c/1

Note that there are only 4 contracts in this table (58,59,60,61). As stated above, each contract has 4 records, each one pointing to a different type of information.

In this case, the query has to return only 1 contract, because only the contract with code 58 that satisfies this condition.

  • Just to bring the total count of the occurrence of these two conditions?? In a single line?

  • That’s right, Tmilitino

  • I am unable to access the link. I think you have some problem in the site. nor the . with this entering

  • Really the service seems to be a bit unstable. For me it’s back now.

  • This phrase: "Staying as follows: (inf_variable code_variable) = 124 and (inf_value)=Great and at the same time (inf_variable code_variable) = 117 and (inf_value)=Systems Analyst" is hard to understand. How "at the same time" can this pair of variables count different data? Define better what you mean by "at the same time".

  • Hello there, anonymous man. is because in the case, instead of me having created four columns, each one being respective for a type of information (profession, note, address and name), I decided to create a separate table for inclusion of the types of information. and then, in this table presented in question, I added all this 4 information in the same column, and each record will specify in a separate column what type is related to the information. So I’m having a hard time treating two conditions when considering that kind of structure.

  • From what I understand is to return or inf_codigo_variavel=124 and inf_valor=Ótimo Or (inf_codigo_variable) = 117 and (inf_valor)=Systems Analyst, that in your example of the link would be 4 lines that obey this condition. the query I used was this select * from tabela where (inf_codigo_variavel=117 and inf_valor="Analista de Sistemas") or (inf_codigo_variavel=124 and inf_valor="Ótimo") and returned me the 4 lines.

  • Actually, Tmilitino, the query has to search for contracts whose score=Otimo and Profession=Systems Analyst. Follow the table by the link above. Note that the result should be 1, because there is only one contract (inf_codigo_contract=58) that satisfies this condition.

  • have to have the same contract? and you want to know how many obey this condition right?

  • This, see that there are four contracts in total (58,59,60,61), and each of them has 4 records, each record corresponding to a type of information

  • Please put that in the question, it’ll be clearer to everyone.

  • I’ll do that.

  • It wouldn’t be the case to rethink your data model?

  • So this structure was designed to meet the dynamic creation of types of information.

  • Mysql may not be the most appropriate DBMS to deploy a value-key database. [https://en.wikipedia.org/wiki/Key-value_database]

Show 10 more comments

4 answers

2

If I understand correctly:

SELECT count(inf_codigo_contrato) FROM sua_tabela
WHERE (inf_codigo_variavel=117 AND inf_valor='ANALISTA DE SISTEMAS') OR (inf_codigo_variavel=124 AND inf_valor='OTIMO');

With your new explanations maybe this query meets:

SELECT count(DISTINCT inf_codigo_contrato) FROM sua_tabela
WHERE EXISTS (SELECT 1 FROM sua_tabela WHERE inf_codigo_variavel=117 AND inf_valor='ANALISTA DE SISTEMAS') AND
      EXISTS (SELECT 1 FROM sua_tabela WHERE inf_codigo_variavel=124 AND inf_valor='OTIMO');

Has really gone incomplete.

SELECT count(DISTINCT inf_codigo_contrato) FROM sua_tabela A
WHERE EXISTS (SELECT 1 FROM sua_tabela B WHERE A.inf_codigo_contrato=B.inf_codigo_contrato AND B.inf_codigo_variavel=117 AND B.inf_valor='ANALISTA DE SISTEMAS') AND
      EXISTS (SELECT 1 FROM sua_tabela C WHERE A.inf_codigo_contrato=C.inf_codigo_contrato AND C.inf_codigo_variavel=124 AND C.inf_valor='OTIMO');
  • Have you tried that one? It was a mistake?

  • I re-asked my question in another way. It is now more explanatory and detailed.

  • I’ll test it here, anonymously, and I’ll be back for feedback.

  • he returned 4 :(

2

Can try this solution, I believe you need two selects to do this, one for each condition.

    SELECT count(t.inf_codigo_contrato) FROM tabela AS t
     WHERE t.inf_codigo_variavel=117 
       AND t.inf_valor='ANALISTA DE SISTEMAS'
       AND t.inf_codigo_contrato IN (
           SELECT inf_codigo_contrato 
             FROM tabela
            WHERE  inf_codigo_variavel=124 
              AND inf_valor='OTIMO'
)
  • I’ll test it now to see.

  • Mto thanks Rafael! It worked here!

1


You can do this in several ways, one of them is by creating two subtables, one that obeys the 1st condition and the other that obeys the 2nd condition. as in the example below.

select count(otimo.inf_codigo_contrato) from 
(select inf_codigo_contrato from tabela where inf_codigo_variavel=117 and inf_valor="Analista de Sistemas") as anSistema 
inner join 
(select inf_codigo_contrato from tabela where inf_codigo_variavel=124 and inf_valor="Ótimo") as otimo 
on anSistema.inf_codigo_contrato=otimo.inf_codigo_contrato

Another option would be to use functions, there you will choose the best for your case, see performance issue and everything else.

  • I’ll test and return =D

  • 1

    He was very sure Tmilitino.. I will mark your answer to this question.. I really appreciate the support!

  • Just stay tuned for the performance of the consultation at the bank, this is paramount

  • I’ll monitor that. Thank you very much, friend.

1

Follow-up:

 SELECT count(*) as qtd_otimo
            FROM tabela t1
                INNER JOIN tabela t2 ON t2.inf_codigo_contrato = t1.inf_codigo_contrato
            WHERE
                t1.inf_codigo_variavel = 117
                AND t1.inf_valor='ANALISTA DE SISTEMAS'
                AND t2.inf_codigo_variavel = 124
                AND t2.inf_valor = 'Ótimo'
            GROUP BY t1.inf_codigo_contrato
  • Thank you John. I also tested your query here, it worked perfectly! Thank you all.

Browser other questions tagged

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