select with multiple AND and OR attributes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SilverRain
    New Member
    • Jan 2008
    • 3

    select with multiple AND and OR attributes

    I am trying to do something fairly simple.

    I have two pages. Brands.php and Products.php.

    When I click on Brands.php it takes me to a list of Brands. I click on a brand, for example, brand1.php. From there I can choose a category - Category1.

    Category1 needs to go to store.php?Brand ID=1&CatID=1. At this point I have only got it to filter by either or... either BrandID or CatID. So I go to store.php?Brand ID=1, I get all items that are under Brand1, and similarly, when I go to CatID=1, I get all items that are under category1.

    The problem is that I cannot get it to filter by BrandID and CatID together. I need something of a combination of the AND/OR fuction in SQL.

    At this point here is what I have:

    SELECT *
    FROM products
    WHERE BrandID = colname OR CatID = colname2

    I have two variables defined: colname and colname2.

    Name: colname
    Type: Numeric
    Default value: -1
    Run-time Value: $_GET['BrandID']

    Name: colname2
    Type: Numeric
    Default value: -1
    Run-time Value: $_GET['CatID']


    This is successfully doing what I have described above, but I need it to filter by each one individually AND both together.

    I have tried:

    SELECT *
    FROM products
    WHERE (BrandID = colname OR CatID = colname2) OR (BrandID= colname AND CatID = colname2)

    but it did not succeed. Do I need to add a new colname3, perhaps?

    I am using Dreamweaver's Advanced Recordset.

    Your help and detailed answer will be greatly appreciated!

    Thanks!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    I would like to know what is the reasoning behind such a select. Because I cannot see any meaning in this.

    this second part of the where clause is completely unnecessary;
    Code:
    BrandID= colname AND CatID = colname2)
    because the result is already determined by the first part of the WHERE clause.

    Maybe I don't understand it. In that case would you please explain why you would want and how you would use thee results of such a select.

    Ronald

    Comment

    Working...