473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Multi-tables LEFT JOIN problem

In MySQL online documentation there are some examples with
multi-tables left joins. But all of them are like this (taken from the
documentation):

SELECT ...
FROM table1
LEFT JOIN table2 on (table1.id = table2.id)
LEFT JOIN table3 on (table1.id2 = table3.id2)
LEFT JOIN table4 on (table1.id3 = table4.id3)

Looks pretty. But in every ON clause there is table1, the same which
is in FROM clause. In other words all tables in joins are related to
the same table (table1). I have different situation: table3 is related
to table2 but not to table1. table4 is related to table3 but not to
table1.

Problem: how to constructs joins with such relations? May be part of
the ON clauses content should be put into WHERE clause? But what is
the rule: logic rule or a thumb rule?

Thank you in advance.

Marek Kotowski
Warsaw
Jul 20 '05 #1
5 2334
Marek Kotowski wrote:
Looks pretty. But in every ON clause there is table1, the same which
is in FROM clause. In other words all tables in joins are related to
the same table (table1). I have different situation: table3 is related
to table2 but not to table1. table4 is related to table3 but not to
table1.


Does this work:

SELECT ...
FROM table1
LEFT JOIN table2 on (table1.id = table2.id)
LEFT JOIN table3 on (table2.id2 = table3.id2)
LEFT JOIN table4 on (table3.id3 = table4.id3);
Jul 20 '05 #2
Aggro <sp**********@yahoo.com> wrote in message news:<vd**************@read3.inet.fi>...
Does this work:

SELECT ...
FROM table1
LEFT JOIN table2 on (table1.id = table2.id)
LEFT JOIN table3 on (table2.id2 = table3.id2)
LEFT JOIN table4 on (table3.id3 = table4.id3);


No. It gives only matching records. In other words
it works like an INNER JOIN.

Regards

Marek Kotowski
Warsaw
Jul 20 '05 #3
Marek Kotowski wrote:
No. It gives only matching records. In other words
it works like an INNER JOIN.


create table table1(id int);
create table table2(id int, id2 int);
create table table3(id2 int, id3 int);
create table table4(id3 int);

insert into table1 values(1),(2),(4),(5),(7),(8);
insert into table2 values(1,21),(2,22),(5,25),(7,27);
insert into table3 values(21,31),(22,32),(27,37);
insert into table4 values(31),(37);

mysql> SELECT * FROM table1
-> LEFT JOIN table2 on (table1.id = table2.id)
-> LEFT JOIN table3 on (table2.id2 = table3.id2)
-> LEFT JOIN table4 on (table3.id3 = table4.id3);
+------+------+------+------+------+------+
| id | id | id2 | id2 | id3 | id3 |
+------+------+------+------+------+------+
| 1 | 1 | 21 | 21 | 31 | 31 |
| 2 | 2 | 22 | 22 | 32 | NULL |
| 4 | NULL | NULL | NULL | NULL | NULL |
| 5 | 5 | 25 | NULL | NULL | NULL |
| 7 | 7 | 27 | 27 | 37 | 37 |
| 8 | NULL | NULL | NULL | NULL | NULL |
+------+------+------+------+------+------+
6 rows in set (0.01 sec)

What kind of results do you need? I understood that you wanted all rows
from table 1, and matching rows from other tables.
Jul 20 '05 #4
Aggro <sp**********@yahoo.com> wrote in message news:<a3***************@read3.inet.fi>...
What kind of results do you need? I understood that you wanted all rows
from table 1, and matching rows from other tables.


Thank you. Your code works fine. But in my RDB
I have to add additional filter for table4.
It has at least one field more, 'name' for example.
So I have to include in the SELECT table4.name = 'something';
Or - in other words - table4.id3 = 'an id for something';
Where to put it? In WHERE or - with an AND operator -
in ON clause and which one (both ways don't work)?

Regards

MK
Jul 20 '05 #5
Marek Kotowski wrote:
Thank you. Your code works fine. But in my RDB
I have to add additional filter for table4.
It has at least one field more, 'name' for example.
So I have to include in the SELECT table4.name = 'something';
Or - in other words - table4.id3 = 'an id for something';
Where to put it? In WHERE or - with an AND operator -
in ON clause and which one (both ways don't work)?


If you want to have ONLY those rows where table4.name = 'something',
then you need to add this at the end of the query (after the joins)

where table4.name = 'something'

# So if we add to my previous example:
alter table table4 add name varchar(255);
update table4 set name='something' where id3=31;
update table4 set name='else' where id3=37;

# We would get this with our previous query:
+------+------+------+------+------+------+-----------+
| id | id | id2 | id2 | id3 | id3 | name |
+------+------+------+------+------+------+-----------+
| 1 | 1 | 21 | 21 | 31 | 31 | something |
| 2 | 2 | 22 | 22 | 32 | NULL | NULL |
| 4 | NULL | NULL | NULL | NULL | NULL | NULL |
| 5 | 5 | 25 | NULL | NULL | NULL | NULL |
| 7 | 7 | 27 | 27 | 37 | 37 | else |
| 8 | NULL | NULL | NULL | NULL | NULL | NULL |
+------+------+------+------+------+------+-----------+

# And this would be our new query:
SELECT * FROM table1
LEFT JOIN table2 on (table1.id = table2.id)
LEFT JOIN table3 on (table2.id2 = table3.id2)
LEFT JOIN table4 on (table3.id3 = table4.id3)
WHERE table4.name='something';

# And this would be the result:
+------+------+------+------+------+------+-----------+
| id | id | id2 | id2 | id3 | id3 | name |
+------+------+------+------+------+------+-----------+
| 1 | 1 | 21 | 21 | 31 | 31 | something |
+------+------+------+------+------+------+-----------+
# I could also show you what would happen if you put that
# new condition into join-clause:

SELECT * FROM table1
LEFT JOIN table2 on (table1.id = table2.id)
LEFT JOIN table3 on (table2.id2 = table3.id2)
LEFT JOIN table4 on (table3.id3 = table4.id3
AND table4.name='something');

+------+------+------+------+------+------+-----------+
| id | id | id2 | id2 | id3 | id3 | name |
+------+------+------+------+------+------+-----------+
| 1 | 1 | 21 | 21 | 31 | 31 | something |
| 2 | 2 | 22 | 22 | 32 | NULL | NULL |
| 4 | NULL | NULL | NULL | NULL | NULL | NULL |
| 5 | 5 | 25 | NULL | NULL | NULL | NULL |
| 7 | 7 | 27 | 27 | 37 | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL | NULL | NULL |
+------+------+------+------+------+------+-----------+

As you can see, we would get all rows for table1, but values for table4
would be null, unless the name='something'. We could use both, but AFAIK
that would be just stupid. And it would give us the same results as it
would with only using where-clause.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: OutsiderJustice | last post by:
Hi All, I can not find any information if PHP support multi-thread (Posix thread) or not at all, can someone give out some information? Is it supported? If yes, where's the info? If no, is it...
37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
2
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource...
14
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
As far as I know, the C Standard has no mention of multi-threaded programming; it has no mention of how to achieve multi-threaded programming, nor does it mention whether the language or its...
4
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= | last post by:
Hi, consider the attached code. Serializing the multi-dimensional array takes about 36s vs. 0.36s for the single-dimensional array. Initializing the multi-dimensional array takes about 4s...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.