Mysql, selecting 4 related tables

Asked

Viewed 258 times

0

I am trying to make a select on 4 different tables. They are all related. It all starts with the Resident.

SELECT morador.*
      ,unidades.*
      ,grupo.*
      ,condominios.*

  FROM morador
  LEFT JOIN unidades
    ON id_morador = morador.id_unidades,
  LEFT JOIN grupo
    ON id_grupo = morador.id_grupo,
  LEFT JOIN condominios
    ON id_condominios = morador.id_condominio

 WHERE morador.id_morador = '1';

Online test: http://sqlfiddle.com/#! 9/537518/5

  • 1

    FRON NO, FROM...

  • 1

    and has a lot of extra comma.

  • @Bacco I edited, even though it has errors. It is the first time I use the LEFT JOIN. Would you be so kind as to edit the link http://sqlfiddle.com/#! 9/537518/5 and indicate the correct form that works? Post the link edited here.

  • 1

    just take the commas after JOIN, and then condos.*

2 answers

4

I think there are some syntax errors, try this.

SELECT 
    morador.*, unidades.*, grupo.*, condominios.*

FROM morador

LEFT JOIN unidades      ON ID_Morador       = morador.id_unidades
LEFT JOIN grupo         ON ID_Grupo         = morador.id_grupo
LEFT JOIN condominios   ON ID_Condominios   = morador.id_condominio

WHERE
    morador.ID_Morador = '1';
  • 1

    Well, that’s basically what I put in the comments, no?

  • Vish I didn’t see the comments dude, sorry... I went straight to the question.

  • 1

    I’m not complaining, I just wanted to make sure it was that or if you thought there was another mistake. I was trying to fix his fiddle, but SQL Fiddle is impossible to use today.

  • 1

    I tried in the fiddle too but it didn’t roll, even difficult, then I looked and seeing with what I saw in my head, it seems correct so. But I couldn’t run the test :/

  • Basically there was a FRON (which he edited in the question later), and the extra commas in select and join.

  • I am now testing directly on Mysql, this seems to be this error #1052 - Column 'ID_Grupo' in on clause is ambiguous. What would be?

  • 1

    Boot alias on all tables, it is a good thing to always use. Avoid future ambiguous also.

  • @Ricardo You can post as it should be?

  • The joins are wrong, it would be good to arrange not to confuse.

  • It would be nice if you didn’t bring everything, but choose the fields.

Show 5 more comments

1


I changed SQL, at first I didn’t see the flaw in jois, you’re trying to make left join of everything with the table morador. I think the broker would be the SQL below, change the alias name to what will best suit you.

    SELECT 
    m.*, a.*, b.*, c.*

FROM morador m

LEFT JOIN unidades      a   ON m.id_unidade       = a.id_unidades
LEFT JOIN grupo         b   ON a.ID_Grupo         = b.id_grupo
LEFT JOIN condominios   c   ON b.id_condominio   = c.ID_Condominios

WHERE
    m.ID_Morador = '1';

I ran a test on your Fiddle with this SQL and it ran.

To use the alias in the columns you cannot use the * you will have to list all columns, which is also the most suitable.

SELECT 
    a.nome as uniNome, b.nome as grupNome

The fiddle isn’t working that way, but that’s the way to do it, look at that link where there is an equal answer to your comment doubt.

  • Strange, now you’re saying you didn’t find the chart unidades http://sqlfiddle.com/#! 9/537518/14

  • Sorry, I commented on the answer. You have to change the Select tb

  • Unknown column 'a.ID_Morador' in 'on clause' what would that be?

  • Take a look at the answer now, reread it, remake it all. I think now it gets where you want it.

  • Ricardo, you’re the man! Thank you very much!

  • Ricardo, can you help me with one more detail? There are some tables that the field has the same name, I need to create an alias, how to do? unidades.nome AS unNome , morador.nome AS moNome, grupo.nome AS grNome

  • You say in the Select, Right? To get the correct column names?

  • Ricardo, yes exactly

  • I updated the answer.

  • All right, thanks again!

Show 5 more comments

Browser other questions tagged

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