473,770 Members | 7,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2361
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**********@y ahoo.com> wrote in message news:<vd******* *******@read3.i net.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**********@y ahoo.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='so mething';

# 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='so mething');

+------+------+------+------+------+------+-----------+
| 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
14576
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 possible to make doing multi-thread stuff? Thanks. YF
37
4896
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, Pujo
12
3879
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
6
8181
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
5767
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 know a good place to start looking for some theory on the subject of multi user applications? I know only bits and pieces, like about transactions, but a compendium of possible approches to multi user programming would be very appreciated!
0
2328
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 Systems (MuCoCoS'08) Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08. <http://www.par.univie.ac.at/~pllana/mucocos08> *********************************************************************** Context
1
9317
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 "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
2
4658
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 file. What is the best way to acheive this with webforms ?
14
3413
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 libraries are suitable for multi-threaded programming. For people who are fond of portable C programming, what's the best way to go about multi-threaded programming? I've been reading up on POSIX threads a little, they seem pretty ubiquitous....
4
7329
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 vs. 0.3s for the single-dimensional array. (I know initializing is not necessary in this simple example,
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10230
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.