SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question select from 2 tables and 1 lookup table ??

    Hi,

    I'm trying to select product data from two tables and filter by category. Here's an idea of the tables:

    products
    product_id model_number
    1
    2
    3
    4
    5

    product_description
    product_id name
    1 lesabre
    2 invicta
    3 impala
    4 caprice
    5 dart

    category_lookup
    product_id category_id
    1 2
    2 3
    3 2
    4 1
    5 3


    My base select to pull the list of all products is this, and it works:

    SELECT * FROM products, product_description WHERE products.product_id > 0 && products.product_id = product_description.product_id ORDER BY model DESC LIMIT 0,30


    But when I try to select only products from a particular category, things go awry. I tried the following, but it resulted in a real slow query and instead of about 30 results I got thousands! (the real database has about 300 items total)

    SELECT * FROM products, product_description, category_lookup WHERE products.product_id > 0 && products.product_id = product_description.product_id AND (products.product.id = category_lookup.product.id AND category_lookup.category_id = 3) ORDER BY model DESC LIMIT 0,30

    thanks for any suggestions.

    -takayuki

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT products.id
         , products.model_number 
         , product_description.name
      FROM category_lookup
    INNER
      JOIN products
        ON products.product.id = category_lookup.product_id
    INNER
      JOIN product_description
        ON product_description.product_id = products.product_id
     WHERE category_lookup.category_id = 3 
    ORDER 
        BY products.model_number DESC LIMIT 0,30
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks r937. I did some reading up on inner joins and i see what's happening.

    your select statement works great, but when I want to list all items (remove the WHERE clause), and not specify a particular category, I get two of each item in the database.

    What should I add to get a full listing without doubles?

    thanks,

    takayuki

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    you're sorting by product model, and if a product belongs to more than one category, you'll see it that many times

    if you just want to list all items, then perhaps use a separate query which doesn't involve the categories
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    roger that. thanks again. i've learned alot!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •