Syntax error in full outer join? - mysql

I have a query where I am using a full outer join. But in some instance,
it gives me a syntax error.
What could be the reason for this? I don't see any miscode in my query.

MySQL does not support full outer join, but you can simulate it as a union between a left and right join query:
SELECT * FROM pbsdev3.item t1
LEFT JOIN pbsdev3.item_ledger_entry t2 ON t1.No_ = t2.Item_No_
UNION ALL
SELECT * FROM pbsdev3.item t1
RIGHT JOIN pbsdev3.item_ledger_entry t2 ON t1.No_ = t2.Item_No_
WHERE t1.No_ IS NULL
Note that in general if you find yourself doing full outer joins often, it could imply that your keys and data model are not well defined. One reason why MySQL does not support full joins could be that you should not have to use it.

Full outer join is quite a pain in MySQL. The first thing I would note is that it should not be needed. The items should match in the two tables, so an inner join or left join should be sufficient:
SELECT i.*, ile.*
FROM pbsdev3.item i LEFT JOIN
pbsdev3.item_ledger_entry ile
ON i.No_ = ile.Item_No_;
If you really need full outer join, then gather together all the items and use left join:
select it.*, ile.*
from (select i.No_ from item i union
select ile.Item_No_ from item_ledger_entry ile
) i left join
item it
on it.No_ = i.No_ left join
item_ledger_entry ile
on ile.No = i.No;

Related

Problem when using full join but not using left join. Why?

When I use a left join on different databases, it works but not when I use inner join. Why ?
SELECT tkblue_tklabel_dev_data.EmailContent.*, tkblue_tklabel_dev_archdata.EmailTracking.*
FROM tkblue_tklabel_dev_data.EmailContent
FULL JOIN tkblue_tklabel_dev_archdata.EmailTracking ON tkblue_tklabel_dev_archdata.EmailTracking.idEmailTracking = tkblue_tklabel_dev_data.EmailContent.idEmailTracking
Unknown table 'tkblue_tklabel_dev_data.EmailContent'
But when using
SELECT tkblue_tklabel_dev_data.EmailContent.*, tkblue_tklabel_dev_archdata.EmailTracking.*
FROM tkblue_tklabel_dev_data.EmailContent
LEFT JOIN tkblue_tklabel_dev_archdata.EmailTracking ON tkblue_tklabel_dev_archdata.EmailTracking.idEmailTracking =
tkblue_tklabel_dev_data.EmailContent.idEmailTracking
I have not this error message, but I can only have the results of the left table. Or, I wish all the results like an full join.
Full join don't exists in mysql but you can produce the same result using both left join and right join in UNION
SELECT tkblue_tklabel_dev_data.EmailContent.*
, tkblue_tklabel_dev_archdata.EmailTracking.*
FROM tkblue_tklabel_dev_data.EmailContent
LEFT JOIN tkblue_tklabel_dev_archdata.EmailTracking
ON tkblue_tklabel_dev_archdata.EmailTracking.idEmailTracking = tkblue_tklabel_dev_data.EmailContent.idEmailTracking
UNION
SELECT tkblue_tklabel_dev_data.EmailContent.*
, tkblue_tklabel_dev_archdata.EmailTracking.*
FROM tkblue_tklabel_dev_data.EmailContent
RIGHT JOIN tkblue_tklabel_dev_archdata.EmailTracking
ON tkblue_tklabel_dev_archdata.EmailTracking.idEmailTracking = tkblue_tklabel_dev_data.EmailContent.idEmailTracking

Getting data differences between two queries

I have two queries that result two result sets i need to compare both the result sets and need to display the differences between them.Hope i will get good support.Thank you.These are my queries
Query:1
SELECT distinct c.sid_ident,c.fix_ident from corept.std_sid_leg as c INNER JOIN (SELECT sid_ident, transition_ident, max(sequence_num) seq, route_type FROM corept.std_sid_leg WHERE data_supplier='J' AND airport_ident='KBOS' GROUP BY sid_ident,transition_ident) b ON c.sequence_num=b.seq and c.sid_ident = b.sid_ident and c.transition_ident =b.transition_ident WHERE c.data_supplier='J' and c.airport_ident='KBOS';
Query:2
SELECT name,trans FROM skyplan_deploy.deploy_sids ON d.name=c.sid_ident WHERE apt = 'KBOS' AND name != trans;
Comparison is to be done on fields sid_ident in corept.std_sid_leg and name in skplan_deplay.deploy_sids. As Mysql does not support full outer join,I thought of using left join and right join and combine both the results.But i stuck up with this.Please help.I am getting syntax error while using left and right join.Thank you.
The following query should simulate a FULL OUTER JOIN in MySQL.
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.NAME = B.NAME
WHERE B.ID IS NULL
UNION ALL
SELECT *
FROM B
LEFT OUTER JOIN A
ON B.NAME = A.NAME
WHERE A.ID IS NULL;
Compare the results of the with an actual FULL OUTER JOIN in SQL Server and you'll see it works.

LEFT OUTER JOIN query not returning expected rows

My aim is to do exactly what a LEFT OUTER JOIN intends to do using the 4th venn diagram: SQL Diagrams:
My query isn't returning any values at all, where in fact, it should be returning all within the Consultant_Memberships minus the one that is stored within Consultant_Memberships_Lists.
Please see the SQL Fiddle for an easier understanding:
SELECT *
FROM consultant_memberships
LEFT OUTER JOIN consultant_memberships_list
ON consultant_memberships.`id` =
consultant_memberships_list.membership_id
WHERE consultant_memberships_list.consultant_id = $id
AND consultant_memberships_list.membership_id IS NULL
The query is using '5' as an ID for demonstration purposes to try and pick out the correct rows.
You current query is basically doing an INNER JOIN because of the consultant_id = 5 on the WHERE clause. I believe you actually want to use:
SELECT *
FROM consultant_memberships m
LEFT OUTER JOIN consultant_memberships_list l
ON m.`id` = l.membership_id
AND l.consultant_id = 5
WHERE l.membership_id IS NULL;
See SQL Fiddle with Demo
Use
SELECT *
FROM consultant_memberships
LEFT Outer JOIN consultant_memberships_list
ON consultant_memberships_list.membership_id = consultant_memberships.`id`
and consultant_memberships_list.consultant_id = 5
where consultant_memberships_list.membership_id IS NULL;
The Where clause used before in your query "consultant_memberships_list.consultant_id = 5 " was neglecting the left outer join.

Difference in MySQL JOIN vs LEFT JOIN

I have this cross-database query...
SELECT
`DM_Server`.`Jobs`.*,
`DM_Server`.servers.Description AS server,
digital_inventory.params,
products.products_id,
products.products_pdfupload,
customers.customers_firstname,
customers.customers_lastname
FROM `DM_Server`.`Jobs`
INNER JOIN `DM_Server`.servers ON servers.ServerID = Jobs.Jobs_ServerID
JOIN `cpod_live`.`digital_inventory` ON digital_inventory.jobname = Jobs.Jobs_Name
JOIN `cpod_live`.`products` ON products.products_pdfupload = CONCAT(digital_inventory.jobname, ".pdf")
JOIN `cpod_live`.`customers` ON customers.customers_id = products.cID
ORDER BY `DM_Server`.`Jobs`.Jobs_StartTime DESC LIMIT 50
it runs fine until I make them LEFT JOINs. I thought that by not specifying a type of join it was assumed to be a LEFT JOIN. Is this not the case?
I thought that by not specifying a type of join it was assumed to be a LEFT JOIN. Is this not the case?
No, the default join is an INNER JOIN.
Here is a visual explanation of SQL joins.
Inner join
Left join
No. When a type isn't specified, an INNER JOIN is used. To read up on differences; wikipedia
I believe the default is INNER JOIN if you just specify JOIN.
If you just mentioned JOIN in query by default it will be considered
as a INNER JOIN.
Left join:Left join will take all the elements from Left table and only matching records from the Right table as Follows.
example:
SELECT column_name(s)
FROM table_name1 #(Left table)
LEFT JOIN table_name2 #(Right table)
ON table_name1.column_name=table_name2.column_name
Hope this helps.

Is it possible to convert this subquery into a join?

I want to replace the subquery with a join, if possible.
SELECT `fftenant_farmer`.`person_ptr_id`, `fftenant_surveyanswer`.`text_value`
FROM `fftenant_farmer`
INNER JOIN `fftenant_person`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_person`.`id`)
LEFT OUTER JOIN `fftenant_surveyanswer`
ON fftenant_surveyanswer.surveyquestion_id = 1
AND fftenant_surveyanswer.`surveyresult_id` IN (SELECT y.`surveyresult_id` FROM `fftenant_farmer_surveyresults` y WHERE y.farmer_id = `fftenant_farmer`.`person_ptr_id`)
I tried:
SELECT `fftenant_farmer`.`person_ptr_id`, `fftenant_surveyanswer`.`text_value`#, T5.`text_value`
FROM `fftenant_farmer`
INNER JOIN `fftenant_person`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_person`.`id`)
LEFT OUTER JOIN `fftenant_farmer_surveyresults`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_farmer_surveyresults`.`farmer_id`)
LEFT OUTER JOIN `fftenant_surveyanswer`
ON (`fftenant_farmer_surveyresults`.`surveyresult_id` = `fftenant_surveyanswer`.`surveyresult_id`)
AND fftenant_surveyanswer.surveyquestion_id = 1
But that gave me one record per farmer per survey result for that farmer. I only want one record per farmer as returned by the first query.
A join may be faster on most RDBMs, but the real reason I asked this question is I just can't seem to formulate a join to replace the subquery and I want to know if it's even possible.
You could use DISTINCT or GROUP BY, as mvds and Brilliand suggest, but I think it's closer to the query's design intent if you change the last join to an inner-join, but elevating its precedence:
SELECT farmer.person_ptr_id, surveyanswer.text_value
FROM fftenant_farmer AS farmer
INNER
JOIN fftenant_person AS person
ON person.id = farmer.person_ptr_id
LEFT
OUTER
JOIN
( fftenant_farmer_surveyresults AS farmer_surveyresults
INNER
JOIN fftenant_surveyanswer AS surveyanswer
ON surveyanswer.surveyresult_id = farmer_surveyresults.surveyresult_id
AND surveyanswer.surveyquestion_id = 1
)
ON farmer_surveyresults.farmer_id = farmer.person_ptr_id
Broadly speaking, this will end up giving the same results as the DISTINCT or GROUP BY approach, but in a more principled, less ad hoc way, IMHO.
Use SELECT DISTINCT or GROUP BY to remove the duplicate entries.
Changing your attempt as little as possible:
SELECT DISTINCT `fftenant_farmer`.`person_ptr_id`, `fftenant_surveyanswer`.`text_value`#, T5.`text_value`
FROM `fftenant_farmer`
INNER JOIN `fftenant_person`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_person`.`id`)
LEFT OUTER JOIN `fftenant_farmer_surveyresults`
ON (`fftenant_farmer`.`person_ptr_id` = `fftenant_farmer_surveyresults`.`farmer_id`)
LEFT OUTER JOIN `fftenant_surveyanswer`
ON (`fftenant_farmer_surveyresults`.`surveyresult_id` = `fftenant_surveyanswer`.`surveyresult_id`)
AND fftenant_surveyanswer.surveyquestion_id = 1
the real reason I asked this question is I just can't seem to formulate a join to replace the subquery and I want to know if it's even possible
Then consider a much simpler example to begin with e.g.
SELECT *
FROM T1
WHERE id IN (SELECT id FROM T2);
This is known as a semi join and if desired may be re-written using (among other possibilities) a JOIN with a SELECT clause to a) project only from the 'outer' table, and b) return only DISTINCT rows:
SELECT DISTINCT T1.*
FROM T1
JOIN T2 USING (id);