MySQL "ON" condition - what does it do?

First of all, let me explain that I was looking for at least an hour for anything related to this MUCH keyword in MySQL. The problem is that any search engine that I use to search for information about it matches the word "ON" in the most trivial and unrelated results. I was also unlucky in looking at the MySQL documentation.

I see when used with an INNER JOIN as a condition, but I have no idea what it does. An example of use is

SELECT t1.name, t2.salary
FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;

I know that the differences between "WHERE" and "HAVING" are the difference between filtering strings and filtering groups, respectively.

I can’t imagine which filters are “ON”.

I figured this might be needed for filtering when using an INNER JOIN, but I used to use WHERE in INNER JOIN cases, for example:

SELECT g.id, g.name, g.date_created, g.date_updated, g.created_by, 
    c.fullname AS creator_name, g.updated_by, u.fullname AS updater_name, 
    COUNT(i.id) as image_count

FROM gallery_groups g INNER JOIN
    users c INNER JOIN
    users u INNER JOIN
    gallery_images i

WHERE g.created_by=c.id AND g.updated_by=u.id AND i.group=g.id
GROUP BY g.name
ORDER BY g.date_updated DESC, g.name

Any information and / or examples would be appreciated!

+3
source share
3 answers

Instead of writing a long WHERE statement, you set the dependency between tables in closing JOIN after the ON keyword

It speeds up the search process in the database (it is controlled by the kartezjan matrix), makes your code cleaner, and it’s easier to set real conditions WHERE

0
source

INNER JOINcan really be written without ON by simply moving all the ON clauses to the where clause. Thanks to a reasonable optimizer, it will not have a difference in performance (but it will make the request understandable anyway!).

(, LEFT JOIN) .

SELECT a.foo, b.bar FROM a LEFT JOIN b ON (a.fk = b.pk)

b.bar = NULL, a.fk b.

SELECT a.foo, b.bar FROM a, b WHERE a.fk.b.pk

, a.fk b.

FYI: http://dev.mysql.com/doc/refman/5.0/en/join.html

+2

FYI, both are identical except INNER JOIN ... ONis ANSI 92 syntax

You probably need google ansi 92 syntax vs ansi 86/89 syntax

0
source

Source: https://habr.com/ru/post/1786072/


All Articles