MySQL joins
Par Pierrick, vendredi 6 juillet 2007 à 12:08 / categorie: Développement / tags: / #101 / rss

As another reminder for myself, here is a list of join examples with MySQL (to compare with Oracle behaviour in previous blog ticket)
mysql> create table mother_table (id int, name varchar(10)); mysql> insert into mother_table (id, name) values (1,'pierrick'); mysql> insert into mother_table (id, name) values (2,'marion'); mysql> create table daughter_table (mother_id int, color varchar(20)); mysql> insert into daughter_table (mother_id, color) values (2, 'pink'); mysql> insert into daughter_table (mother_id, color) values (3, 'green'); mysql> select * from mother_table; +------+----------+ | id | name | +------+----------+ | 1 | pierrick | | 2 | marion | +------+----------+ 2 rows in set (0.00 sec) mysql> select * from daughter_table; +-----------+-------+ | mother_id | color | +-----------+-------+ | 2 | pink | | 3 | green | +-----------+-------+ 2 rows in set (0.01 sec) mysql> select * mysql> from mother_table mysql> join daughter_table on mother_id = id mysql> ; +------+--------+-----------+-------+ | id | name | mother_id | color | +------+--------+-----------+-------+ | 2 | marion | 2 | pink | +------+--------+-----------+-------+ 1 row in set (0.00 sec) mysql> select * mysql> from mother_table mysql> left join daughter_table on mother_id = id mysql> ; +------+----------+-----------+-------+ | id | name | mother_id | color | +------+----------+-----------+-------+ | 1 | pierrick | NULL | NULL | | 2 | marion | 2 | pink | +------+----------+-----------+-------+ 2 rows in set (0.00 sec) mysql> select * mysql> from mother_table mysql> left inner join daughter_table on mother_id = id mysql> ; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join daughter_table on mother_id = id' at line 1 mysql> select * mysql> from mother_table mysql> left outer join daughter_table on mother_id = id mysql> ; +------+----------+-----------+-------+ | id | name | mother_id | color | +------+----------+-----------+-------+ | 1 | pierrick | NULL | NULL | | 2 | marion | 2 | pink | +------+----------+-----------+-------+ 2 rows in set (0.00 sec) mysql> select * mysql> from mother_table mysql> right join daughter_table on mother_id = id mysql> ; +------+--------+-----------+-------+ | id | name | mother_id | color | +------+--------+-----------+-------+ | 2 | marion | 2 | pink | | NULL | NULL | 3 | green | +------+--------+-----------+-------+ 2 rows in set (0.00 sec) mysql> select * mysql> from mother_table mysql> right inner join daughter_table on mother_id = id mysql> ; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join daughter_table on mother_id = id' at line 1 mysql> select * mysql> from mother_table mysql> right outer join daughter_table on mother_id = id mysql> ; +------+--------+-----------+-------+ | id | name | mother_id | color | +------+--------+-----------+-------+ | 2 | marion | 2 | pink | | NULL | NULL | 3 | green | +------+--------+-----------+-------+ 2 rows in set (0.00 sec) mysql> select * mysql> from mother_table mysql> cross join daughter_table mysql> ; +------+----------+-----------+-------+ | id | name | mother_id | color | +------+----------+-----------+-------+ | 1 | pierrick | 2 | pink | | 2 | marion | 2 | pink | | 1 | pierrick | 3 | green | | 2 | marion | 3 | green | +------+----------+-----------+-------+ 4 rows in set (0.01 sec)
Commentaires
Aucun commentaire pour le moment.
Ajouter un commentaire
Les commentaires pour ce billet sont fermés.