Jump to content

Recommended Posts

I'm quite sure my syntax is wrong, but I don't know how it should be...so I'll just show you what works and what doesn't:

 

this works:

SELECT t1.category_name AS lev1, t2.category_name AS lev2, t3.category_name AS lev3, t3.category_id AS catid
FROM categories AS t1
LEFT JOIN categories AS t2 ON t2.parent_id = t1.category_id
LEFT JOIN categories AS t3 ON t3.parent_id = t2.category_id
WHERE t1.category_name = 'KNIVES'

 

This does not work:

SELECT t1.category_name AS lev1, t2.category_name AS lev2, t3.category_name AS lev3, t3.category_id AS catid, m1.SectionCode AS mid1
FROM categories AS t1, products AS m1
LEFT JOIN categories AS t2 ON t2.parent_id = t1.category_id
LEFT JOIN categories AS t3 ON t3.parent_id = t2.category_id
WHERE t1.category_name = m1.SectionCode

 

My error says:

Unknown column 't1.category_id' in 'on clause'

 

But that doesn't really help me figure out why it's not working....

Link to comment
https://forums.phpfreaks.com/topic/216830-mysql-left-join-to-self-and-other-table/
Share on other sites

Ok, after a bit of research I've given this another shot, with different results.  Now it's just timing out....

I should add in here, MySQL version 5.0.91, running this through phpMyAdmin 3.3.3.

 

 

SELECT t1.category_name AS lev1, t2.category_name as lev2, t3.category_name as lev3, t3.category_id, t1.parent_id
FROM categories AS t1
LEFT JOIN categories AS t2 ON t2.parent_id = t1.category_id
LEFT JOIN categories AS t3 ON t3.parent_id = t2.category_id
LEFT JOIN products AS m1 ON t1.category_name = m1.SectionCode
LEFT JOIN products AS m2 ON t2.category_name = m2.ClassCode
LEFT JOIN products AS m3 ON t3.category_name = m3.Family
WHERE t1.parent_id = '1';

 

By the way, I have one top level category with parent_id = '0'.  Otherwise all top levels have parent_id = '1'

I figured it out...I should have used parenthesis around my tables.  This works for me:

SELECT 
t1.category_name AS lev1, 
t2.category_name AS lev2, 
t3.category_name AS lev3, 
t3.category_id AS catid, 
m1.SectionCode AS mid1
FROM (categories AS t1, products AS m1)
LEFT JOIN categories AS t2 ON t2.parent_id = t1.category_id
LEFT JOIN categories AS t3 ON t3.parent_id = t2.category_id
WHERE t1.parent_id = '1'
AND m1.Family = t3.category_name
AND m1.ClassCode = t2.category_name
AND m1.SectionCode = t1.category_name

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.