1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Please suggest query

Discussion in 'MySQL' started by JEET, Dec 14, 2007.

  1. #1
    Hi,
    (php and mysql)
    Below are 2 tables I got

    Categories:
    catid parent
    1 0
    2 0
    3 1 (sub cat of 1)
    4 1 (sub cat of 1)

    Products:
    prodid catid
    1 1
    2 1
    3 2
    4 4


    Here's the problem.
    I'm getting the "catid" from a form. These catid's are only of categories which have parent set to 0. So form may send 1 or 2 to script

    Now I got to select all "prodid" from "products" table where catid is either the number sent by form, AND also "prodid" where it's a sub cat of number sent by form.

    So if I get "1" from form, my query should be able to select:

    products:
    1 1
    2 1
    4 4 (4 is a sub cat of 1 in "categories")

    What should be the query?
    Thank you :)
     
    JEET, Dec 14, 2007 IP
  2. ferrari61

    ferrari61 Peon

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I recommend that you create your tables in MS Access - then create a query in MS Access - it should be reasonably easy - you'll get exactly what you are looking for!
     
    ferrari61, Dec 14, 2007 IP
    JEET likes this.
  3. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    I don't know how to make tables and queries in ms-access... I'm using php and mysql...

    I can get the sub categories listed in an array, and then get "prodid" for each, but that would slow the script and increase queries...
    Thanks
     
    JEET, Dec 14, 2007 IP
  4. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Something like this.
    
    select p.prodid from products p where p.catid=$catid or p.catid in (select catid from Categories where parent=$catid)
    
    PHP:
     
    codesome, Dec 14, 2007 IP
    JEET likes this.
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Hi,
    Thanks that worked I think. Green reputation added. :)
     
    JEET, Dec 15, 2007 IP