More elaborate queries in Mysql

Asked

Viewed 135 times

2

I have a problem in my application have the following tables

Responsavel:
id       | responsavel_email
1        | joã[email protected]
2        | [email protected]
3        | [email protected]
4        | [email protected]

Aluno:
id       | nome_aluno      |classe_id
1        | Marli           | 1
2        | Joel            | 2
3        | Alonso          | 2

relacionamento:
    id       | Aluno_id        |responsavel1_id |responsavel2_id
    1        | 1               | 1              | 3
    2        | 2               | 2              | 4

I needed to do an SQL where I talk I want emails from Casse 2’s parents sql respondesse

relacionamento:
        id       | Aluno_id        |responsavel1_id |responsavel2_id
        1        | Joel            | [email protected]|[email protected]

I came to this consultation ..

    SELECT
   responsavel.nome_responsavel,
   relacionamento.responsavel1
FROM
   responsavel
INNER JOIN
   relacionamento ON responsavel.id = relacionamento.responsavel1

but now I need WHERE

  • Gabriel, put the code you’re trying to do so we can help, the way you posted you’re asking me to do it for you and not to help you...

  • I have no idea how to do, I know how to make simple queries , I tried to do using this example Select * table Inner Join table2 WHERE field = 0 and field2 = '' and field3 or field10 is NULL

  • I managed to get to that query in q it gives me a general, now I needed to try to do a WHERE with the Student Class SELECT responsavel.email, relationship.responsavel1 FROM responsavel INNER JOIN relationship ON responsavel.id = relationship.responsavel1

1 answer

1


Try this script:

SELECT A.id, A.nome_aluno, RES1.responsavel_email, RES2.responsavel_email
FROM aluno A
INNER JOIN relacionamento R ON R.Aluno_id = A.id
LEFT JOIN responsavel RES1 ON RES1.id = R.responsavel1_id
LEFT JOIN responsavel RES2 ON RES2.id = R.responsavel2_id
WHERE A.classe_id = [ID da classe]
  • Really this helped, I was struggling to make the next Join , until one I could ... Thanks bro !

Browser other questions tagged

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