Not sure on Left Join, Outer Join, Unions or Minus? [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Current work requires my to have a complex level of SQL syntax including these examples.
What in short is a left Join and where would you use it?

What ever you have written is a JOIN clause that is used to combine rows from two or more tables, based on a related column between them.
Including outer join or left join depends on the table structure and the requirement to pull data from those tables.
IF you are looking for what is left join ,
Left join returns everything from the left table and the matching records from the right table and if there is no match in right table the result is null for the right table
Let us know what are you looking for

Related

What is the difference in MySQL between JOIN ON and WHERE? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is the difference in MySQL between:
SELECT * FROM A JOIN B ON A.item=B.item
and
SELECT * FROM A, B WHERE A.item=B.item
Multi FROM is similar to JOIN operation, you have to use WHERE clause to limit the rows returned. And it correspond to an implicit join.
But as you can read in comments, you should use explicit JOIN (OUTER JOIN, INNER JOIN, LEFT JOIN, RIGHT JOIN) that determine how the data is "linked" in ON clause.

how to join two tables when matching entry doesn't exist [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I am having some trouble writing this SQL Query.
Basically, I have the following tables;
received_flight_files_table & uploaded_flight_files_table
And the following fields;
Processing_Month, IATA_Code, Airline, Received_Date, Uploaded_Date
I would like the query to produce something like the first attached screenshot.
The 2 date fields you can see are from different tables though.
The join can be done on the IATA_Code and Airline fields as both of these values should be the same in both tables.
Here are 2 examples of statements I have written which aren't quite right;
SELECT DISTINCT received_flight_files_table.Processing_Month, received_flight_files_table.IATA_Code, received_flight_files_table.Airline, received_flight_files_table.Received_Date, uploaded_flight_files_table.Uploaded_Date, published_flight_table.Published_Date
FROM ((received_flight_files_table
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Processing_Month=received_flight_files_table.Processing_Month)
INNER JOIN published_flight_table ON published_flight_table.Published_Month=received_flight_files_table.Processing_Month
WHERE received_flight_files_table.Processing_Month = [enter MMMYY];`
SELECT DISTINCT received_flight_files_table.Processing_Month, received_flight_files_table.IATA_Code, received_flight_files_table.Airline, received_flight_files_table.Received_Date, uploaded_flight_files_table.Uploaded_Date
FROM (((received_flight_files_table
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Processing_Month=received_flight_files_table.Processing_Month
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.IATA_Code=received_flight_files_table.IATA_Code
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Airline=received_flight_files_table.Airline)))
WHERE (received_flight_files_table.Processing_Month = [enter MMMYY];
There should sometimes be some blanks in the Uploaded_Date field as for example - in one table I might have received the files but not yet uploaded them.
Try to use LEFT JOIN instead of INNER JOIN.
https://www.diffen.com/difference/Inner_Join_vs_Outer_Join

Group by MySQL with special case [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have two tables called Master Websites and Defacement Monitor
Master website Table
Defacement Monitor Table
The master_website table contains all the website in the system, and defacement monitor status table filled with status of the each website. We can add status of a website with any date and time statuses are 'safe', 'defaced', 'broken', 'normal', What I want is count of the 'safe','normal', 'broken' with considering only the last entry of each website.
Any help please?
Sorry for my bad english
Thanks in advance
You can join the tables together, and then do a LEFT OUTER JOIN against the monitor table looking for a newer row. Using the WHERE clause you can ignore any row where a newer one is found.
Like this:-
SELECT dm1.defacement_status, COUNT(dm1.id)
FROM master_website_table m
INNER JOIN defacement_monitor_table dm1
ON m.id = dm1.website_id
LEFT OUTER JOIN defacement_monitor_table dm2
ON m.id = dm2.website_id
AND dm1.defacement_datetime < dm2.defacement_datetime
WHERE dm2.id IS NULL
GROUP BY dm1.defacement_status
Note that if any status is not currently used then it will not appear in the results (ie, you will not get a count of 0). It would be better to have a table containing the possible status' (then your monitor table could just use the id from this table instead of the text of the status).

Mysql left join query returns values NULL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My query:
SELECT DISTINCT img.prd_id,prd.*,img.* FROM products_prd prd OUTER JOIN
prd_images_img img ON prd.prd_id=img.prd_id ORDER BY prd.prd_datetime
the primary key in products_prd is returned NULL. They have values. while img.prd_id may be NULL or not.
The problem is that you have name collisions -- two things called prd_id for example. You need to use aliases to rename the columns:
SELECT prd.*, img.col1 as img_col1, img.col2 as img_col2
FROM products_prd prd LEFT OUTER JOIN
prd_images_img img
ON prd.prd_id = img.prd_id
ORDER BY prd.prd_datetime;
You don't need to select prd_id twice.
Or, you can use USING instead of ON:
SELECT *
FROM products_prd prd LEFT OUTER JOIN
prd_images_img img
USING (prd_id)
ORDER BY prd.prd_datetime;
This returns the columns in the USING clause only once (although you could have problems with other columns if they have the same names in the two tables).

Select all rows from MySQL table with non-existent relation as empty cell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If I had for example two tables called 'teachers' and 'lessons' and 'lessons' has a foreign key 'teacher_ID' referring to its counterpart in the 'teachers' table, then how would I select all rows from teacher with all of their corresponding lessons with an empty cell if no lesson is connected to them? I only was able to make MySQL show me the teachers that have one or more lessons attached.
Is this even possible without LEFT JOIN? I couldn't find anything on Google...
EDIT
I was interested in the mechanics of the LEFT JOIN keyword. But since there doesn't seem to be an alternative I'd say case closed.
The right way is using LEFT JOIN. This way if not match you will get (teacher_id), null
The LEFT JOIN keyword returns all the rows from the left table (teachers), even if there are no matches in the right table (lessons).
SELECT teacher.teacher_ID, lesson.lesson_ID
FROM teachers
LEFT JOIN lessons
ON teacher.teacher_ID = lesson.teacher_ID
If you want to emulate LEFT JOIN first use JOIN to find the element with match. And use UNION to add the rest with a value of NULL
SELECT teacher.teacher_ID, lessons.lesson_ID
FROM teachers
JOIN lessons
ON teacher.teacher_ID = lessons.teacher_ID
UNION
SELECT teacher.teacher_ID, null as lesson_ID
FROM teachers
WHERE NOT EXISTS ( SELECT 1
FROM lessons
WHERE lessons.teacher_id = teacher.teacher_id)