How do I make a SELECT in 2 or more Tables with 2 or more conditions?

Asked

Viewed 58,526 times

9

I have a problem making a select with the following conditions: I want to show the data either with condition1 or with condition2

Select * tabela inner join tabela2 WHERE campo = 0 and campo2 = '' and campo3 or campo10 is NULL

Select all fields in the table where condition 1 or with condition 2.

Concrete example:

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)
//Quero acrescentar esta segunda condição
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 
  • Specify better what you are trying to do, which table the fields belong to and which you want to select under which conditions...

  • show the data of these select. With two possible conditions to be shown

  • Gives error? does not come the expected data? What is the problem?

  • I don’t see the expected data. So I guess there’s something wrong with the condition

  • @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 conditions AND.

  • 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

  • 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

Show 2 more comments

1 answer

26


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.

  • My problem wasn’t exactly how the query was going, but how I was asking for the data. Whenever I have dates placed (Accidentalness='' OR Accidentalness is Null OR Accidentalness='0000-00-00') .

  • 7

    One answer after another, all consistently excellent. Although the problem of the PO was elsewhere, the explanation is so detailed that there is no way not to learn a bit and to proceed alone towards a solution. + 1 is a little :)

Browser other questions tagged

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