select populated categories

    Date: 07/08/05 (MySQL Communtiy)    Keywords: php

    hey guys, this is my first post in here - i'm also a member of php and flashdev

    this seems simple, but i hope i don't bungle it up - i don't have a concrete example for this, just something i've been thinking about

    let's say i have 2 tables, categories and items and there exists items.categoryID and category.ID;

    is there an easy way to select only the categories that have associated items?


    for instance:

    if i have:
    categories
    ----------
    1. Category A
    2. Category B
    3. Category C
    4. Category D

    (1, 2, 3 and 4 being categories.id)

    and

    items
    -----
    1. Item A (items.categoryID = 1)
    2. Item B (items.categoryID = 4)


    can I, with just 1 select query, pull ONLY Category A (id = 1) and Category D (id = 4) to say, build a menu of categories, selecting only those categories that are "populated"?

    in the past, how i've done this has been select ALL categories, then one at a time loop through the categories and do a SELECT statement for each ID and check for at least 1 result; then flag each category that has associated "stuff"; this is allright if the page you're building has to list all the products AND categories, but let's say I just want to build a menu of only available categories and not display any items (or not ALL the items) on a given page

    any help is appreciated; let me know if that makes any sense...


    Source: http://www.livejournal.com/community/mysql/61688.html

« One-to-many join? || Query help - Most recent date »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home