Jump to content

Adding another table to a multitable query


Buchead

Recommended Posts

Hello.........

I've been presented with a query that works and have been asked to expand what it does. Unfortunately I know what it should do but not how to code it.

The database has 4 tables: "products" which contains product info. "product_tree" which lists the productID from "products" and a sectionID. "advancedpricing" which contains info regarding pricing. "sections" which contain the sectionID and relationships between sections.

The currect query is:

select products.* ,advancedpricing.price1 as advPrice1 from products LEFT JOIN advancedpricing ON products.productID=advancedpricing.productID and advancedpricing.priceType=0 and (advancedpricing.accTypeID=0 or advancedpricing.accTypeID=1) ) where (products.productID > 1) and (accTypes like '%;1;%' or accTypes like '%;0;%') and (visible = 'Y') and visible = "Y" and ((code like "%$search%" or name like "%$search%" or description like "%$search%" or keywords like "%$search%")) group by products.productID order by name LIMIT 0,10

This works, but I've been asked to have it so it only searches for products in a given section, and it's sub-sections, so adding the products_tree table into the mix. I've tried adding "products_tree.sectionID=$section" after the where identifier, and also products_tree before and after the LEFT JOIN, but it fails. I've resorted to seemingly adding obviously incorrect code into the query in a bid to get it working!

Can anyone point out where I'm going wrong?

Many thanks,

Clive.
Link to comment
Share on other sites

If everything currently works as it should then adding

[code]
INNER JOIN product_tree ON products.productID = product_tree.productID
[/code]

after "from products" and then adding "AND sectionID = $section" after "(products > 1)" should work. I recommend you test the current query and the new query throroughly
Link to comment
Share on other sites

That does the job (thanks), however I also need to do a check in the sections table for any parent/child relationships. products_tree lists all the products and which section they go in, while in sections, it lists all the sections, their sectionID and the sectionID of their parent.

So if main = 1, sub1 = 2, sub2 = 3, and sub3 is beneath sub1 it will have a sectionID of 4 but with a parent of 2 - the others will have a parent of 1.

Thanks!
Link to comment
Share on other sites

[quote author=Buchead link=topic=112054.msg454646#msg454646 date=1161290034]
That does the job (thanks), however I also need to do a check in the sections table for any parent/child relationships. products_tree lists all the products and which section they go in, while in sections, it lists all the sections, their sectionID and the sectionID of their parent.

So if main = 1, sub1 = 2, sub2 = 3, and sub3 is beneath sub1 it will have a sectionID of 4 but with a parent of 2 - the others will have a parent of 1.

Thanks!
[/quote]

Is there a limit on the depth of the subcategories that can be made or is it unlimited? For instance could sectionID 4 have a subcategory and then could that subcategory have another subcategory ....?
Link to comment
Share on other sites

With the current limit you can use the following to retrieve the section ids of subcategories. Note that This could possibly be worked into the original query.
[code]
<?php
$section = (int)$section;
$section_ids = array($section);
$query = 'SELECT '
        .'sectionID '
        .'FROM '
        .'sections AS s'
        .'WHERE '
        .'s.parentID  = '.$section;
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result))
{
    while ($row = mysql_fetch_assoc($result))
    {
        $section_ids[] = $row['sectionID'];
    }

}
$section_ids = implode(',', $section_ids);
?>
[/code]

You can use the following to retrieve the section_ids of the subcategories regardless of depth.
[code]
<?php
$section_ids = array();
$new_ids = array((int)$section);
do
{
    $section_ids = array_merge($section_ids, $new_ids);
    $query = 'SELECT sectionID FROM sections WHERE parentID IN ('.implode(',', $new_ids).')';
    $result = mysql_query($query) or die(mysql_error());

    $new_ids = array();
    if (mysql_num_rows($result))
    {
        while ($row = mysql_fetch_assoc($result))
        {
            $new_ids[] = $row['section_id'];
        }
    }
} while (count($new_ids));

$section_ids = implode(',', $section_ids);
?>
[/code]

In each case you'll change "AND sectionID = $section" to "AND sectionID IN ($section_ids)".

You should have a test for whether or not the section is 1, in which case you'd generate a query that either  removes the sectionID comparison in the WHERE clause or makes it "AND sectionID" only or completely removes the JOIN on table "sections", removing the related WHERE conditions as well.

EDIT: Btw, you'll probably want a multicolumn index on (parent, section) and (section, parent)
Link to comment
Share on other sites

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.