Jump to content

Only get unique rows


xiao

Recommended Posts

I have this query:

$result = mysql_query("SELECT p.products_id, p.products_model, p.products_price, p.products_image, pd.products_name, pc.categories_id FROM products p, products_description pd, products_to_categories pc WHERE pc.categories_id = 10 AND pd.language_id = 1 AND p.products_price <=50 ORDER BY p.products_price") or die(mysql_error());

 

What it should do:

Get id, model, price, image and name for all products with category 10 that have a price <= 50

 

I'm using it in osCommerce so I had to include the language_id part to only get the english ones.

But now with the above query I get product like 5 or 6 times (might be more), and I don't know how to fix that...

I tried some GROUP BYs, but that seems to give incorrect rows.

Link to comment
Share on other sites

This seems to work a little better:

$result = mysql_query("SELECT p.products_id, p.products_model, p.products_price, p.products_image, pd.products_name, pc.categories_id FROM products p, products_description pd, products_to_categories pc WHERE pc.categories_id = 10 AND p.products_id = pd.products_id AND pd.language_id = 1 AND p.products_price <=50 GROUP BY pd.products_name ORDER BY p.products_price") or die(mysql_error());

 

But it appears to give products with any category_id

Link to comment
Share on other sites

is this an improvement?

$result = mysql_query("SELECT DISTINCT p.products_id, p.products_model, p.products_price, 
        p.products_image, pd.products_name, pc.categories_id 
        FROM products p, products_description pd, products_to_categories pc 
        WHERE pc.categories_id = 10 
            AND pd.language_id = 1 
            AND p.products_price <=50 
        ORDER BY p.products_price") 
        or die(mysql_error());

Link to comment
Share on other sites

Whoa!

 

I just noticed you have no join criteria specified

 

$result = mysql_query("SELECT DISTINCT p.products_id, p.products_model, p.products_price, 
        p.products_image, pd.products_name 
        FROM products p
        INNER JOIN products_description pd ON p.products_id = pd.products_id
        WHERE p.categories_id = 10 
            AND pd.language_id = 1 
            AND p.products_price <=50 
        ORDER BY p.products_price") 
        or die(mysql_error());

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.