Join SQL server

Asked

Viewed 73 times

1

Good afternoon, I started working with sql to 3 mesês and I’m picking up on an application.

I have 3 Tables which I have information associated with by "registration"

I would like to get all the information from the first table called configuration even if it is not associated with the other divides and records.

sql = "SELECT * 
    FROM dividas a 
    LEFT JOIN registros r on (a.registro = r.registro)  
    INNER JOIN configuracao b on r.registro = b.registro 
    WHERE a.empresa='" & LEFT(Combo1.Text, 2) & "' 
    ORDER BY r.registro"

In this query it brings the information if you have record information in the 3 Tables, I want you to bring also the information you only have in "configuration" even if you do not have in "debts" and "records"

If anyone can help me.

Thank you!

  • You can show us the relationship between the tables?

  • As I do not know the template I will add here, if solve game in response, try the following qury: Select 
 * 
from configuracao b
 LEFT Join registros r on r.registro = b.registro 
 LEFT join dividas a on a.registro = r.registro
where a.empresa='" & Left(Combo1.Text, 2) & "' 
order by r.registro

  • 1

    Rodrigo, it is VERY important to understand the JOINS (LEFT, INNER, RIGHT, FULL). The ideal first of all, would be you understand them, try to do (which I’m sure you’ll get). This statement "if you have record information in the 3 Tables" may be incorrect, because you have a "LEFT" of the table registros, then bring the independent records if you have or not in the table dividas. Count on us to help you. Look at this JOINS table that will help you: Link 1, Link 2

  • Good morning, first of all thanks for the tips.. I tried the query and it brought me the same answer, I will send the grid of each table: configuration (which is the one I want to bring everything) Note: The records 3700 and 3800 are only in this table and these are the ones that do not appear in the others. REG NAME DT ADM STATUS DT DEM PAG 3000 BELTRANO X Y Z B 3700 NOMECONFIG X Y Z B 3800 NOMECONFIG2 X Y Z B 3300 NOMEQUALQUER X Y Z B In the table records and divides comes all information less the 3700 and 3800. ñ managed to send an image to explain better, I can send via email if it becomes difficult to understand

1 answer

0


Considering only the code you passed, you can try the following query:

sql = "SELECT * 
    FROM configuracao b
    LEFT JOIN dividas a on b.registro = a.registro and a.empresa='" & LEFT(Combo1.Text, 2) & "' 
    LEFT JOIN registros r on (a.registro = r.registro)  
    ORDER BY r.registro"

Just move configuracao to be the main table of the query. Making a LEFT JOIN with the other tables; that is, if there is any occurrence in the table of configuracao it will bring the value returned.

But this change also forces you to move your WHERE a.empresa='" & LEFT(Combo1.Text, 2) & "' to the Join table dividas (if we maintain the condition it will only bring a return if there is someone in dividas that meets the condition).

Just as an observation, the query of the question makes a left Join with registros and a Inner Join with configuracao; in other words, it brings a return if there are occurrences in dividas and in configuracao (only not being necessary occurrence in the table registros).

  • Good people, I was able to solve the problem with this query sql = "SELECT ? FROM configuration b LEFT JOIN divided a on (b.registro = a.registro) and a.empresa='" & Left(Combo1.Text, 2) & "' LEFT JOIN records r on (b.registro = r.registro) ORDER BY b.registro" It was in the kick, I haven’t analyzed it yet to understand better because I need to finish this application soon. Thank you all !!!

  • @Rodrigomerce, apparently the tip I gave solved your problem. So, you can mark the answer as correct. If you need any extra help to understand the solution, just call!

  • I can comment here another question of the same query or I need to open another question ?

  • ideally it is good to open another question, not to "pollute". I just read that question that talks about leaving the posts cleaner ;)

Browser other questions tagged

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