Join SQL server

1

Good afternoon, I started working with sql 3 months ago and I'm getting caught up in an application.

I have 3 tables which I have information associated with the "registry"

I would like to get all the information from the first table named configuration even though it is not associated with the other tables 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 log information in the 3 tables, I want you to also bring the information that only has in "configuration" even if it does not have in "debits" and "records"

If anyone can help me.

Thank you!

    
asked by anonymous 15.08.2018 / 22:36

1 answer

0

Considering only the code that you have 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.

However, this change also forces its condition from WHERE a.empresa='" & LEFT(Combo1.Text, 2) & "' to join of table dividas (if we keep the condition it will only return if there is someone in dividas that meets the condition).

Just as a remark, the question query does a left join with registros and an inner join with configuracao ; in other words, it returns if there are occurrences in dividas and configuracao (only occurring in the registros table).

    
16.08.2018 / 13:06