Mysql 6.2: LEFT/RIGHT JOIN
Written by: maffelu , 2009-04-16 21:20:27
|--------------------+--------------------|
|id |name |
|____________________+____________________|
|1 Maffelu |
|2 Wimpy |
|3 Gh0ztK1ll4h |
|4 Nobou |
+-----------------------------------------+
|--------------------+--------------------|
|userId |time |
|____________________+____________________|
|2 15:29 |
|2 18:29 |
|1 05:03 |
|4 09:20 |
+-----------------------------------------+
SELECT users.id AS UID, users.name AS Name, logins.time AS Time
FROM users
LEFT JOIN logins
ON users.id = logins.userId
|--------------------+--------------------+--------------------|
|UID |Name |Time |
|____________________+____________________+____________________|
|1 Maffelu 05:03 |
|2 Wimpy 15:29 |
|2 Wimpy 18:29 |
|3 Gh0ztK1ll4h NULL |
|4 Nobou 09:20 |
+--------------------------------------------------------------+
|--------------------+--------------------|
|id |name |
|____________________+____________________|
|1 Maffelu |
|2 Wimpy |
|3 Gh0ztK1ll4h |
|4 Nobou |
+-----------------------------------------+
|--------------------+--------------------|
|userId |time |
|____________________+____________________|
|2 15:29 |
|2 18:29 |
|1 05:03 |
|4 09:20 |
+-----------------------------------------+
SELECT users.id AS UID, users.name AS Name, logins.time AS Time
FROM users
RIGHT JOIN logins
ON users.id = logins.userId
|--------------------+--------------------+--------------------|
|UID |Name |Time |
|____________________+____________________+____________________|
|2 Wimpy 15:29 |
|2 Wimpy 18:29 |
|1 Maffelu 05:03 |
|4 Nobou 09:20 |
+--------------------------------------------------------------+
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.