Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do 3-table Natural Join?

Tags:

sql

mysql

Is this the correct way of writing three natural joins? :

SELECT C.name, P.name 
FROM client C NATURAL JOIN order O NATURAL JOIN product P 
WHERE O.date > '15.02.2011'
like image 587
Yunus Eren Güzel Avatar asked Feb 28 '11 20:02

Yunus Eren Güzel


2 Answers

To check syntax when your SQL product of choice does not support it, use the Mimer SQL-92 validator. You should discover that order and date are reserved words. Change them to my_order and my_date respectively and you will then discover yours is valid Transitional SQL-92 syntax.

like image 68
onedaywhen Avatar answered Sep 20 '22 15:09

onedaywhen


This is indeed the typical syntax for natural joins. However, not all databases support natural joins (for instance, I don't believe SQLServer supports it) and I don't believe there is an ANSI standard for natural join.

Note that natural joins are generally considered dangerous and something to be avoided - this is because they obscure the join relationship that a query depends on, and could result in queries whose meaning changes if the data model is altered.

like image 31
LBushkin Avatar answered Sep 21 '22 15:09

LBushkin