user1923086 user1923086 - 1 month ago 13
MySQL Question

SQL Query, how to get all elements from one list and only the similar ones from another table

I have 2 tables, and they have PartIDs as their similarity to branch them. I need to pull 3 columns from the first table and 2 columns from Table 2.

The problem I am having is that Table 1 sometimes has more or different PartIDs than Table 2 and that is OK. In a third table I want to be able to have a query that can run and give me all the PartIDs from the first table and second table even if they dont exist in the other one. But if they exist in both I would like the data to be together as it normally is

This code shows the PartIDs that are similar between both Tables:

SELECT `Sheet1$`.ENSPIECEID, `Sheet1$`.`Part Number`, `Sheet1$`.Description,`Sheet1$`.TITRETYPE, `Sheet1$`.Utilization, `Sheet2$`.Notes, `Sheet2$`.Justification
FROM `Sheet1$` `Sheet1$`, `Sheet2$` `Sheet2$`
WHERE `Sheet1$`.ENSPIECEID = `Sheet2$`.ENSPIECEID


I need this so that I can flag which PartIDs are coming only from 1st sheet and which only from 2nd Sheet.

Answer
SELECT `Sheet1$`.ENSPIECEID, `Sheet1$`.`Part Number`, `Sheet1$`.Description,`Sheet1$`.TITRETYPE, `Sheet1$`.Utilization, `Sheet2$`.Notes, `Sheet2$`.Justification
FROM `Sheet1$` `Sheet1$`
LEFT JOIN `Sheet2$` `Sheet2$` on `Sheet1$`.ENSPIECEID = `Sheet2$`.ENSPIECEID

would return all records from sheet1 and only those in sheet 2 that match; assuming there are no other syntax errors.