Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-10-2008, 02:03 PM
cmgmyr
 
Posts: n/a
Default Complex Query

Hey All,
I have a little delema that I can't figure out. I have this query
currently:

SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
t3.name as category3, p.description, p.price, p.sort
FROM
categories AS t1
LEFT JOIN
categories AS t2 ON t2.parentid = t1.id
LEFT JOIN
categories AS t3 ON t3.parentid = t2.id
INNER JOIN
products AS p ON t3.id = p.category

Now, this works great if there are 3 levels of categories, but I need
this to be altered to show all of the items if there are 1, 2, or 3
levels of categories...and not just 3. Any ideas?

This is kinda what i'm looking for (even though it doesn't work)

SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
t3.name as category3, p.description, p.price, p.sort
FROM
categories AS t1
LEFT JOIN
categories AS t2 ON t2.parentid = t1.id
LEFT JOIN
categories AS t3 ON t3.parentid = t2.id
IF t3.id <> NULL INNER JOIN products AS p ON t3.id = p.category
ELSE IF t2.id <> NULL INNER JOIN products AS p ON t2.id = p.category
ELSE INNER JOIN products AS p ON t1.id = p.category

Thanks in advance,
-Chris
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-10-2008, 02:03 PM
Rik Wasmus
 
Posts: n/a
Default Re: Complex Query

On Thu, 08 May 2008 20:40:04 +0200, cmgmyr <cmgmyr@gmail.com> wrote:

> Hey All,
> I have a little delema that I can't figure out. I have this query
> currently:
>
> SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
> t3.name as category3, p.description, p.price, p.sort
> FROM
> categories AS t1
> LEFT JOIN
> categories AS t2 ON t2.parentid = t1.id
> LEFT JOIN
> categories AS t3 ON t3.parentid = t2.id
> INNER JOIN
> products AS p ON t3.id = p.category
>
> Now, this works great if there are 3 levels of categories, but I need
> this to be altered to show all of the items if there are 1, 2, or 3
> levels of categories...and not just 3. Any ideas?
>
> This is kinda what i'm looking for (even though it doesn't work)
>
> SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
> t3.name as category3, p.description, p.price, p.sort
> FROM
> categories AS t1
> LEFT JOIN
> categories AS t2 ON t2.parentid = t1.id
> LEFT JOIN
> categories AS t3 ON t3.parentid = t2.id
> IF t3.id <> NULL INNER JOIN products AS p ON t3.id = p.category
> ELSE IF t2.id <> NULL INNER JOIN products AS p ON t2.id = p.category
> ELSE INNER JOIN products AS p ON t1.id = p.category


With minor changes:

SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
t3.name as category3, p.description, p.price, p.sort
FROM categories AS t1
LEFT JOIN categories AS t2 ON t2.parentid = t1.id
LEFT JOIN categories AS t3 ON t3.parentid = t2.id
INNER JOIN products AS p ON p.category = COALESCE(t3.id,t2.id,t1.id);
--
Rik Wasmus
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-10-2008, 02:03 PM
cmgmyr
 
Posts: n/a
Default Re: Complex Query

On May 8, 4:39*pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Thu, 08 May 2008 20:40:04 +0200, cmgmyr <cmg...@gmail.com> wrote:
> > Hey All,
> > I have a little delema that I can't figure out. I have this query
> > currently:

>
> > SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
> > t3.name as category3, p.description, p.price, p.sort
> > FROM
> > categories AS t1
> > LEFT JOIN
> > categories AS t2 ON t2.parentid = t1.id
> > LEFT JOIN
> > categories AS t3 ON t3.parentid = t2.id
> > INNER JOIN
> > products AS p ON t3.id = p.category

>
> > Now, this works great if there are 3 levels of categories, but I need
> > this to be altered to show all of the items if there are 1, 2, or 3
> > levels of categories...and not just 3. Any ideas?

>
> > This is kinda what i'm looking for (even though it doesn't work)

>
> > SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
> > t3.name as category3, p.description, p.price, p.sort
> > FROM
> > categories AS t1
> > LEFT JOIN
> > categories AS t2 ON t2.parentid = t1.id
> > LEFT JOIN
> > categories AS t3 ON t3.parentid = t2.id
> > IF t3.id <> NULL INNER JOIN products AS p ON t3.id = p.category
> > ELSE IF t2.id <> NULL INNER JOIN products AS p ON t2.id = p.category
> > ELSE INNER JOIN products AS p ON t1.id = p.category

>
> With minor changes:
>
> SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
> t3.name as category3, p.description, p.price, p.sort
> *FROM categories AS t1
> LEFT JOIN categories AS t2 ON t2.parentid = t1.id
> LEFT JOIN categories AS t3 ON t3.parentid = t2.id
> INNER JOIN products AS p ON p.category = COALESCE(t3.id,t2.id,t1.id);
> --
> Rik Wasmus- Hide quoted text -
>
> - Show quoted text -


Thank you very much for that. It's very close. The only problem is, is
that if an item has 2 categories it shows as 2 rows, if it has 3
categories, it shows 3 times. Any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-10-2008, 02:03 PM
PleegWat
 
Posts: n/a
Default Re: Complex Query

On Thu, 08 May 2008 15:08:31 -0700, cmgmyr wrote:

> On May 8, 4:39Â*pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>> SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
>> t3.name as category3, p.description, p.price, p.sort
>> Â*FROM categories AS t1
>> LEFT JOIN categories AS t2 ON t2.parentid = t1.id LEFT JOIN categories
>> AS t3 ON t3.parentid = t2.id INNER JOIN products AS p ON p.category =
>> COALESCE(t3.id,t2.id,t1.id);

>
> Thank you very much for that. It's very close. The only problem is, is
> that if an item has 2 categories it shows as 2 rows, if it has 3
> categories, it shows 3 times. Any ideas?


Use SELECT DISTINCT rather than SELECT

--
Remove caps to reply
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-10-2008, 02:03 PM
cmgmyr
 
Posts: n/a
Default Re: Complex Query

On May 9, 1:45*pm, PleegWat <pleegwat.REM...@CAPS.telfort.nl.INVALID>
wrote:
> On Thu, 08 May 2008 15:08:31 -0700, cmgmyr wrote:
> > On May 8, 4:39*pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> >> SELECT p.id, p.styleno, t1.name AS category1, t2.name as category2,
> >> t3.name as category3, p.description, p.price, p.sort
> >> *FROM categories AS t1
> >> LEFT JOIN categories AS t2 ON t2.parentid = t1.id LEFT JOIN categories
> >> AS t3 ON t3.parentid = t2.id INNER JOIN products AS p ON p.category =
> >> COALESCE(t3.id,t2.id,t1.id);

>
> > Thank you very much for that. It's very close. The only problem is, is
> > that if an item has 2 categories it shows as 2 rows, if it has 3
> > categories, it shows 3 times. Any ideas?

>
> Use SELECT DISTINCT rather than SELECT
>
> --
> Remove caps to reply


Thanks for the reply. I did try that with some other alterations:

SELECT DISTINCT(p.id), p.styleno, t1.name AS category1, t2.name as
category2, t3.name as category3, p.description, p.price, p.sort
FROM categories AS t1
LEFT JOIN categories AS t2 ON t2.parentid = t1.id
LEFT JOIN categories AS t3 ON t3.parentid = t2.id
INNER JOIN products AS p ON p.category = COALESCE(t3.id,t2.id,t1.id)
GROUP BY(p.id)
ORDER BY p.id

This is coming up with 604 records total (there are actually 607
products) I did some looking and it's not pulling any items that are
assigned to a sub-category with sub-sub-categories for some reason.

Category 1
- Category 2 <~~~ Not pulling these products
-- Category 3

Is there any reason for this?

Thanks!
-Chris
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 06:01 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145