473,809 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Same Table in LEFT JOIN possible?

I have this working fine in Access and MSSQL. While trying to convert
to MySQL the result is not what I'm looking for to get..

Two tables:

T1
----------------
ID1|cName |
----------------
10 |Electronics|
----------------
20 |Computers |
---------------

T2
-------------------
ID2 |cName |ID1|
-------------------
11 |CD |10 |
-------------------
12 |DVD |10 |
-------------------
13 |VCR |10 |
-------------------
14 |TV |10 |
-------------------
21 |HP |20 |
-------------------
22 |COMPAQ |20 |
-------------------
23 |IBM |20 |
-------------------
24 |DELL |20 |
------------------
221 |Desktop |22 |
------------------
222 |Server |22 |
------------------
111 |Sony |11 |
------------------
112 |JVC |11 |
------------------
The Query is:

strsql = "SELECT TB1.cName, TB2.cName TB3.cName "
strsql = strsql & "FROM T1 AS TB1 "
strsql = strsql & "LEFT JOIN (T2 AS TB2 LEFT JOIN T2 AS TB3 ON TB3.ID1
= TB2.ID2) "
strsql = strsql & "ON TB2.ID1 = TB1.ID1"
strsql = strsql & "ORDER BY TB1.cName, TB2.cName, TB3.cName;"
The Resulted format from Access/MSSQL (well after filtering):

Lev 1 * - Electronics

Lev 2 ** - CD
Lev 3 *** - SONY
Lev 3 *** - JVC
Lev 2 ** - DVD
Lev 2 ** - VCR
Lev 2 ** - TV

Lev 1 * - Computers

Lev 2 ** - HP
Lev 2 ** - Compaq
Lev 3 *** - Desktop
Lev 3 *** - Server
Lev 2 ** - IBM
Lev 2 ** - DELL

On MySQL I have ALL content from T2 listed under each entry from T1.
Not sure if LEFT JOIN of the same table (T2 AS TB2 LEFT JOIN T2 AS
TB3) is legal, or it's a problem in nesting... Can someone help?
Jul 20 '05 #1
2 6871
Vector wrote:
I have this working fine in Access and MSSQL. While trying to convert
to MySQL the result is not what I'm looking for to get..

Two tables:
[snip]
The Query is:

strsql = "SELECT TB1.cName, TB2.cName TB3.cName "
strsql = strsql & "FROM T1 AS TB1 "
strsql = strsql & "LEFT JOIN (T2 AS TB2 LEFT JOIN T2 AS TB3 ON TB3.ID1
= TB2.ID2) "
strsql = strsql & "ON TB2.ID1 = TB1.ID1"
strsql = strsql & "ORDER BY TB1.cName, TB2.cName, TB3.cName;"


The Resulted format from Access/MSSQL (well after filtering):
[snip]
On MySQL I have ALL content from T2 listed under each entry from T1.
Not sure if LEFT JOIN of the same table (T2 AS TB2 LEFT JOIN T2 AS
TB3) is legal, or it's a problem in nesting... Can someone help?


Try it like this instead (untested)

SELECT TB1.cName, TB2.cName**TB3. cName
FROM T1 AS TB1
LEFT JOIN T2 AS TB2 ON TB2.ID1 = TB1.ID1
LEFT JOIN T2 AS TB3 ON TB3.ID1 = TB2.ID2
ORDER BY TB1.cName, TB2.cName, TB3.cName

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 20 '05 #2
Thank you Chris! Worked like a charm...
Chris Hope <bl*******@elec trictoolbox.com > wrote in message news:<11******* ******@216.128. 74.129>...

Try it like this instead (untested)

SELECT TB1.cName, TB2.cName TB3.cName
FROM T1 AS TB1
LEFT JOIN T2 AS TB2 ON TB2.ID1 = TB1.ID1
LEFT JOIN T2 AS TB3 ON TB3.ID1 = TB2.ID2
ORDER BY TB1.cName, TB2.cName, TB3.cName

Jul 20 '05 #3

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

Similar topics

0
2486
by: Charles Haven | last post by:
Hello all, Is it possible to LEFT JOIN a table to itself? I want to see all records in a table where the year of the sales date is 2003 and where the salesman sold an item to a customer to which he did not sell that item to in 2002. The following is an example table (SALES):
1
4631
by: dan | last post by:
i have 2 tables that i want to count TABLE 1 categories --id --name forum_comments --id --category_id
1
7222
by: Jim | last post by:
Im trying to find the error in this statement: CREATE PROCEDURE STP_selectmain AS select a.inventoryid, b.firstname, b.lastname, art.title, art.medium, a.cost, a.inventoryid, a.receivedate, a.dimensions, a.reference, art.provenance, sum(c.restorationcost),
3
6475
by: honda | last post by:
My main table has the following structure: t1 (id_primary, id_secundary, name) i.e. I want to join this table with the following second table: t2 (id_primary, id_secundary, value) i.e. The join should first try to find a match on id_primary and only if that fails it should find a match on id_secundary. Every row in t1 is matched
6
3764
by: pb648174 | last post by:
I have a pivot table implementation, part of which is posted below. It returns no errors in query analyzer, but when profiler is run, it shows that "Error 208" is happening. I looked that up in BOL and it means that an object doesn't exist. This block of code below works fine on my local development machine, but not on our shared development server until I go into the tempdb and make the user have the role db_owner. Even wierder is that...
7
12855
by: Yannick Turgeon | last post by:
Hello all, I'm using SS2K on W2k. I'v got a table say, humm, "Orders" with two fields in the PK: OrderDate and CustomerID. I would like to add an "ID" column which would be auto-increment (and would be the new PK). But, I would really like to have orders with the oldest OrderDate having the smallest ID number and, for a same OrderDate, I'd to have the smallest CustomerID first. So my question is:
4
2274
by: Dia | last post by:
Hi there, I struggle to get this going i would like to insert data into 2 tmp tables in a view. If i run the code on it's own it works perfectly until i want to create a view it complains about the INSERT this is my code
2
5282
by: filmar | last post by:
I have two tables and need to recieve counts of each groups in columns. The 1st table is necessary to recieve exactly 4 rows even if there no one match condition id the 2nd table. It have to count if checked is equal to 1. CREATE TABLE . ( NOT NULL , ) ON GO CREATE TABLE . (
9
9230
by: shanevanle | last post by:
I have two tables that are pretty big. I need about 10 rows in the left table and the right table is filtered to 5 rows as well. It seems when I join the tables in the FROM clause, I have to filter my left table in the WHERE clause and cannot filter it in the FROM clause. This seems like it would cause a lot of overhead especially when my left table is ten thousand rows. Am I wrong in thinking that the two tables get joined with the...
0
10639
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...
1
10383
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7661
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
6881
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
5550
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
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.