473,385 Members | 2,015 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,385 software developers and data experts.

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 6849
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*******@electrictoolbox.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
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...
1
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
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,...
3
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. ...
6
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...
7
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...
4
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...
2
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...
9
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.