How to make separate sorts in tables with Left Join?

Asked

Viewed 50 times

1

I need the following help from you.

First, see below my current query:

sq1 = mysqli($conexao,"select * from tabela1 TBL1 left join tabela2 TBL2 ON
         TBL1.codigo=TBL2.codigoestrangeiro LEFT JOIN tabela3 TBL3 ON
         TBL2.codigo=TBL3.codigoestrangeiro
         group by TBL1.codigo order by TBL1.codigo desc");

Table 2 and table 3 may contain n records linked to table 1. In this case, how do I call for this information?

And how do I sort the information in a personalized way, IE, table 2 must be displayed in order of name, and table 3 by date.

  • related: http://answall.com/questions/143668/convertendo-dois-selects-mysql-para-um

  • Again, as I mentioned in the other question, avoid multiple questions in one. Lei the guide to use the site: http://answall.com/tour

  • @Danielomine, even if it is two questions, it would not be very creative to create another question just to know how to order a query.

  • Read the site usage guide, please.

  • @Luis, your select is almost certain, I just don’t know what the need to use the group by?

  • to order: order by TBL1.codigo desc, TBL2.nome ASC, TBL3.data ASC

  • for n records use left join, to pick up unique records, use inner join. Obs: the group by, will group the records, so it will not bring all relations, filtering the other records, will only appear "the first record you find" for each code.

  • Hello, Marco, if I don’t use group by, it will repeat records of the same code from table 1. If table 2 has 3 records, it would show the code of table 1 repeated 3 times.

  • I got it Daniel, sorry again.

Show 4 more comments
No answers

Browser other questions tagged

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