+ 3

Why do we use ON instead of WHERE when using LEFT JOIN?

14th Apr 2018, 7:10 PM
Tony Stout
Tony Stout - avatar
2 odpowiedzi
0
They are different things: WHERE is used for you to filter with a value for example if you want users older than 45 years or called Tony Stout. ON is used to compare fields in two tables in a JOIN For example if you have a table called answers which has a field called userId and an users table with an id column you can say that you want to JOIN the tables ON answers.userId=users.id WHERE users.id=2 and with this you get all the answers from the user with id 2. I know that the example could be achieved without any JOIN but hope you have understood anyway ;)
15th Apr 2018, 7:50 PM
Pedro Duarte
Pedro Duarte - avatar
0
The simplified answer is that “ON” defines the value both tables have in common so you can stitch (JOIN) the two tables together. WHERE is the available clause to filter down other specifics to refine the exact details you wish to receive. A logical example in real life would be: “You and I meet for lunch ON your Birthday”. If you are a table and I am a table, what is bringing (joining) us together? The birthday, that is the common value we are sharing. You are traveling from a different location than I am and we likely have information, or stories that differ from one another. Using “ON” allows us to come together in one location (result set) but have information from two different sources. WHERE would be used in addition to joining two tables if you wanted to say something like, you only want to meet up where the time is = 12:00pm (or any other variable piece of information you wish to use) otherwise you simply know you are to meet on your Birthday without refined specifics. Hopefully this helps someone. This is my first time answering a question on any site that I can remember. ✌️
2nd Jul 2018, 11:05 AM
Chris
Chris - avatar