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?
– Tmilitino
That’s right, Tmilitino
– Luis
I am unable to access the link. I think you have some problem in the site. nor the . with this entering
– Tmilitino
Really the service seems to be a bit unstable. For me it’s back now.
– Luis
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".
– anonimo
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.
– Luis
From what I understand is to return or
inf_codigo_variavel=124
andinf_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 thisselect * 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.– Tmilitino
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.
– Luis
have to have the same contract? and you want to know how many obey this condition right?
– Tmilitino
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
– Luis
Please put that in the question, it’ll be clearer to everyone.
– Tmilitino
I’ll do that.
– Luis
It wouldn’t be the case to rethink your data model?
– anonimo
So this structure was designed to meet the dynamic creation of types of information.
– Luis
Mysql may not be the most appropriate DBMS to deploy a value-key database. [https://en.wikipedia.org/wiki/Key-value_database]
– anonimo