Your concrete example is a little confused and seems to have little to do with the logic you’re presenting.
Your logical example
Since you are not giving details about each table and how they relate, I can leave some tips to build the query based on the logic presented:
Select * table Inner Join table2 WHERE field = 0 and campo2 = '' and campo3 or campo10 is NULL
You must alias the tables to correctly identify where the columns are:
SELECT t1.* FROM minhaTabela t1 WHERE t1.meuCampo = 0
We’re saying that the column meuCampo
is on the table minhaTabela
through the alias t1
.
If you intend to use OR
, should involve the sub-condition in ()
:
SELECT t1.*, t2.campoXpto # selecciona tudo da t1 e campos da t2
FROM minhaTabela t1 # identifica minhaTabela como t1
INNER JOIN minhaTabela2 t2 ON (t1.campo = t2.campo) # match com tabela t2
WHERE t1.meuCampo = 0 # condição #1
AND t1.meuCampo2 = '' # condição #2
AND (t1.meuCampo3 IS NULL OR t1.meuCampo10 IS NULL) # condição #3
We are saying in condition #3 that the meuCampo3
or the meuCampo10
has to be NULL
.
Your royal consultation
Your query in the form you have in question is confused and does not clarify where the columns are or how you intend to validate the information to collect or not the records.
You should see the database as a person you’re having a conversation with and ask questions:
Database, give me records of table X where there are columns that have Y values and have a relation with table J!
Even after I have identified your query, there are many doubts:
SELECT *
FROM tb_detalhe_trabalhador
INNER JOIN tb_trabalhador ON tb_detalhe_trabalhador.id = tb_trabalhador.id
INNER JOIN tb_equipamentos ON tb_detalhe_trabalhador.id = tb_equipamentos.id
WHERE AlvaraNumero = 0
AND (AlvaraValidade='' or AlvaraValidade is Null OR AlvaraValidade='0000-00-00')
AND (AlvaraAnexo='' OR AlvaraAnexo is Null)
AND AcidenteNumero = 0
AND (AcidenteValidade='' OR AcidenteValidade is Null OR AcidenteValidade='0000-00-00')
AND (AcidenteAnexo='' OR AcidenteAnexo is Null)
AND SeguroNumero = 0
AND (SeguroValidade='' OR SeguroValidade is Null OR SeguroValidade='0000-00-00')
AND (SeguroAnexo='' or SeguroAnexo is Null)
AND InstaladorNumero = 0
AND (InstaladorValidade='' OR InstaladorValidade is Null OR InstaladorValidade='0000-00-00')
AND (InstaladorAnexo='' OR InstaladorAnexo is Null)
OR MedicaValidade is NULL
OR MedicaAnexo is NULL
OR ProjectistaNumero is NULL
OR ProjectistaValidade is NULL
OR ProjectistaAnexo is NULL
OR GasNumero is NULL
OR GasValidade is NULL
OR GasAnexo is NULL
OR SoldadorNumero is NULL
OR SoldadorValidade is NULL
OR SoldadorAnexo is NULL
OR MecanicoValidade is NULL
OR MecanicoNumero is NULL
OR MecanicoAnexo is NULL
OR ClasSoldadorNumero Is NULL
OR ClasSoldadorValidade is NULL
OR ClasSoldadorAnexo is NULL
ORDER BY tb_trabalhador.id
To which table the fields belong:
When you query more than one table, you should always indicate which table belongs to the column:
tb_detalhe_trabalhador.meuCampo
or alias t1
to the table tb_detalhe_trabalhador
:
t1.meuCampo
Type X or Y conditions shall always be protected by ()
to avoid misinterpretation thereof:
The last conditions are OR
, but they were of ()
which means you want records where X and Y and Z or J.
J will subscribe to X and Y and Z.
Basically you’re saying that if one of those columns in OR
for NULL
, you want the record.
Specify better what you are trying to do, which table the fields belong to and which you want to select under which conditions...
– RodrigoBorth
show the data of these select. With two possible conditions to be shown
– ChrisAdler
Gives error? does not come the expected data? What is the problem?
– jean
I don’t see the expected data. So I guess there’s something wrong with the condition
– ChrisAdler
@user3253195 My answer aims to improve your understanding on how to put your query. But I assume from your current consultation that your problem is in its last stages
OR
which will end up agreeing to the conditionsAND
.– Zuul
really can’t understand what exactly you’re trying to ask, but @Zuul’s reply should be able to answer any questions you may have
– RodrigoBorth
Edit: sorry, now that I saw. the question was posted 7 months ago. vacillated :v I read the date on the American model, which coincides with today, 3/11
– Olimon F.