How to Convert Left Outer JOIN Query to Subset Query - mysql

how to convert this left outer join query to subset query
select
j.*, concat(d.Name, ', ', d.Gelar) as DSN,
prg.Nama_Indonesia as PRG,
kl.Kelas as kls,
kl.Sesi as ssi
from
jadwal j left outer join
dosen d on j.IDDosen=d.ID left outer join
kelas kl on j.Kelas=kl.ID left outer join
program prg on j.Program=prg.Kode left outer join
jabatanorganisasi jo on d.JabatanOrganisasi=jo.Kode left outer join
tahun t on j.tahun=t.id
order by
d.Name, prg.Nama_Indonesia, kl.Sesi, kl.Kelas;
please help
give me axample

For the first query you will be having rows, since all of the joins are LEFT JOIN, so atleast all entries from table jadwal will be there in output.
But on the other query you are doing selection using conditions(works like INNER JOIN), if the condition not satisfies there wont be any result-set.Thats why you are not getting any outputs.
There is no data with these conditions.Please check data
where
j.IDDosen=d.ID and
j.kelas=kl.ID and
j.program=prg.kode and
j.tahun=t.id AND
d.JabatanOrganisasi=jo.kode
Hope this helps

In the first query, you are using left joins (which means if there's no match between from table and the joining tables it would retrieve all the results from the from table), and in the second query you are using inner joins.
You can take a look on this What is the difference between "INNER JOIN" and "OUTER JOIN"?

Related

What is the difference in mySQL AND vs Where

I'm using mysql and I confused with "And", "Where"
Somby dy can tell me what is difference between these.
SELECT *,COUNT(comment.id) as comment_count from posts LEFT JOIN comment on posts.post_id =comment.post_id AND comment.approve = 1 GROUP BY posts.post_id
SELECT *,COUNT(comment.id) as comment_count from posts LEFT JOIN comment on posts.post_id =comment.post_id WHERE comment.approve = 1 GROUP BY posts.post_id
They are not the same, first one will return the associations for all, and the second will do it just for the rows in the where match.
In this other duplicate question you can see the full explanation and examples
SQL JOIN - WHERE clause vs. ON clause
Simply change the query to use an inner join like this:
select tableA.id, tableA.name, tableB.details
from tableA
inner join tableB ...
here is the definition of left join:
The LEFT JOIN (also called LEFT OUTER JOIN) keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).
whereas the definition of the inner join is:
The INNER JOIN keyword return rows when there is at least one match in both tables.

Why LEFT JOIN is terribly slower than INNER JOIN?

I have two tables, toynav_product_import - 18533 rows, catalog_product_entity - 42000 rows.
The below query, LEFT JOIN takes more than 2 minutes, while INNER JOIN runs in 0.009 seconds. The first table has the necessary index for the barcode field.
SELECT tpi.barcode FROM toynav_product_import tpi
INNER JOIN catalog_product_entity cpe ON tpi.barcode = cpe.sku
Please advise
toynav_product_import
catalog_product_entity
An outer join ( LEFT JOIN or RIGHT JOIN ) has to do all the work of an INNER JOIN plus the extra work of null-extending the results
And even if a LEFT JOIN were faster in specific situations, it is not functionally equivalent to an INNER JOIN, so you cannot simply go replacing all instances of one with the other!
Sorry cant post this as a comment
A LEFT JOIN is slower than the Inner Join. By definition, an outer join (LEFT JOIN or RIGHT JOIN) has to do all the work of an INNER JOIN plus the extra work of null-extending the results, Thats the reason. And as it also returns more number of Rows as compare to inner join, Thats why execution takes more time.
But by indexing the Foreign Keys properly, you can definitely increase the performance of the Joins.
It also depends on the Data, Its not always the case that Left join is slower, There are the cases when Left join is faster, But mostly Inner join is faster according to above described reasons.
Please refer to this link, the guy explained the difference very clearly.

SQL request using join

I have 2 tables:
circuit(id_circuit, distance)
and
circuit_langue(id_circuit_language, #id_circuit, language, title).
if I do a join between circuit and circuit_langue, and it's possible that some objects from circuit don't have a circuit_langue,
what i have to do if I want to recuperate objects without circuit_langue ?
You most likely did INNER JOIN which shows only those records that have a matching row from both sides of the join (tables in this example).
You need a LEFT JOIN to view all circuits even if there is not circuit_langue row associated with it:
select *
from circuit c
left join circuit_langue cl on
c.id_circuit = cl.id_circuit
If you only need to display records that don't have a corresponding row in langue table you could add a WHERE condition to above query:
select *
from circuit c
left join circuit_langue cl on
c.id_circuit = cl.id_circuit
where cl.id_circuit is null
By default, the JOIN (INNER JOIN) recovers rows that match on both tables.
If you want to recover both the objects with and without a circuit_langue associated you can use a LEFT OUTER JOIN:
SELECT * FROM circuit c LEFT OUTER JOIN circuit_langue cl
ON c.id_circuit = cl.id_circuit
There are four main kinds of joins :
inner join (which is the default)
left outer join
right outer join
full outer join
You've used the INNER JOIN which only returns the matched values in both tables, while you actually need a LEFT OUTER JOIN which returns all rows from the left table, even if there are no matches in the right table.
For reference, the left table is the first one after the FROM keyword.
For further knowledge:
RIGHT OUTER JOIN : is exactly the opposite of LEFT OUTER JOIN.
FULL OUTER JOIN :returns rows when there is a match in either one of the tables.

How to do multiple joins when some tables are empty

I am trying to append 'lookup data' to a main record text/description field so it looks something like this:
This is the Description Text
LookUpName1:
LookupValue1
LookupValueN
This worked fine with Inner Join like so
Select J.id, Concat('<b>LookUpName</b>:<br>',group_concat(F.LookUpValue SEPARATOR '<br>'))
from MainTable J Inner Join
LookUpTable L Inner Join
LookUpValuesTable F
On J.ID = L.JobID and F.ID = L.FilterID
Group by J.ID
However my goal is to add append multiple Lookup Tables and if I add them to this as Inner Joins I naturally just get those record where both/all the LookupTables have records.
On the other hand when I tried Join or Left Join I got an error on the Group by J.ID.
My goal is to append any of the existing Lookup Table values to all of the Description. Right now all I can achieve is returning appended descriptions which have ALL of the Lookup table values.
Your query would work if the on clauses were in the "right" place:
select J.id,
Concat('<b>LookUpName</b>:<br>', group_concat(F.LookUpValue separator '<br>'))
from MainTable J left join
LookUpTable L
on J.ID = L.JobID left join
LookUpValuesTable F
on F.ID = L.FilterID
group by J.ID;
The problem with your query is a MySQL (mis)feature. The on clause is optional for an inner join. Don't ask me why the MySQL designers thought inner join and cross join should be syntactically equivalent. Every other database requires an on clause for an inner join. It is easy enough to express a cross join using on 1=1.
However, the on clause is required for the left join, so when you switch to a left join, the compiler has a problem with the unorthodox syntax. The real problem is a missing on clause; this just happens to show up as "I wasn't expecting a group by yet." Using more traditional syntax with each join followed by an on should fix the problem.

Issue SQL join query

I am getting issues with the below SQL query, unable to fetch the desired result
SELECT
C.Department__c
,C.Email
,R.AcctID__c
,C.ContactID__c
,R.TransactionDueDate
,R.PubNbr__c
FROM RenewalNotificationProgramDE R
LEFT JOIN ContactNewDE C
ON R.AcctID__c = C.AcctID__c
The idea is to join all the AccountID(AcctID__c) in RenewalNotificationProgramDE table to the corresponding contacts in ContactNewDE table. AccountID is the foreign key in the ContactNewDE table. I am usig Innerjoin in my query as I want all AccountID to map with their corresponding contacts in ContactNewDE.
Just replace LEFT JOIN by INNER JOIN to get all AccountID with their corresponding contacts :-
Use below query :-
SELECT
C.Department__c
,C.Email
,R.AcctID__c
,C.ContactID__c
,R.TransactionDueDate
,R.PubNbr__c
FROM RenewalNotificationProgramDE R
INNER JOIN ContactNewDE C
ON R.AcctID__c = C.AcctID__c
You have used LEFT JOIN in your query, which you need to change it to INNER JOIN. I think you would better to notice the below notes about differences between left and inner joins:
INNER JOIN: Returns all rows when there is at least one match in BOTH
tables
LEFT JOIN: Return all rows from the left table, and the
matched rows from the right table