0

I have two tables with the following information

PRODUCTS
id     |description       |featureImage
13     |a phone           | www.image.com/image23
14     |a phone accessory | www.image.com/image24
15     |another accessory | www.image.com/image25


Product Accessories
id     |productId         |accessoryId
1      |13                |14
2      |13                |15

When using a query as follows

 SELECT DISTINCT ON (accessories.id, products.description)
        products.description, products.featureImage, accessories.id 
 FROM schema.accessories 
 JOIN schema.products ON products.id IN 
   (SELECT accessories.accessoryId FROM accessories
    WHERE accessories.id IN (14,15))

I am getting duplicate results as

 description        |featureImage     |id
 a phone accessory  |image.com/image24|14
 a phone accessory  |image.com/image24|15
 another accessory  |image.com/image25|14
 another accessory  |image.com/image25|15

The result set I was expecting would be

description         |featureImage     |id
a phone accessory   |image.com/image24|14
another accessory   |image.com/image25|15

So I would search the table for products with the original product id, and then get the ids of the rows with accessories for that product. Once I get those they are used for the sub query

(SELECT accessories.accessoryId FROM accessories WHERE accessories.id IN (14,15))

to get the results.

I was thinking that using the DISTINCT ON would stop the duplicates, but I am still getting duplicates, with the id being linked up incorrectly on some of them.

Question is solved, just need to wait for the 8 hours so I can reply and close as answered with the following text;

as per @a_horse_with_no_name by simplifying the query to

SELECT p.description, p.featureimage, a.accessory_id
FROM accessories a 
JOIN products p ON p.id = a.accessory_id

with a Where statement at the end identifying the proper product to find accessories for, the expected result set is returned.

10
  • So what would I be looking to do if I wanted to only have the product details, with the id of the row in the accessory table for that accessory? So I would only get a single 'a phone accessory' and a single 'another accessory' with the appropriate id for the row containing that product id as the accessory? Commented Apr 14, 2014 at 14:56
  • doing DISTINCT ON (accessories.id) returns me a result set with the appropriate different ids, but the description, and featureImage both come back as 'phone acessory' with its featureImage url. Commented Apr 14, 2014 at 15:09
  • 1
    A distinct on only makes sense if you also use an order by. But your join condition looks pretty strange as well. What exactly are you trying to achieve here? Commented Apr 14, 2014 at 15:18
  • 2
    Isn't that a simple join between products and accessories based on accessory.accessoryId = product.id Commented Apr 14, 2014 at 15:24
  • 1
    sqlfiddle.com/#!15/7f460/1 Commented Apr 14, 2014 at 15:27

2 Answers 2

1

From the discussion in the comments it seems all you want is a simple join:

SELECT p.description, p.featureimage, a.accessory_id
FROM accessories a 
   JOIN products p ON p.id = a.accessory_id

SQLFiddle example: http://sqlfiddle.com/#!15/7f460/1

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

SELECT
    ,p.description
    ,p.featureImage
    ,a.id
FROM SCHEMA.accessories AS a
JOIN SCHEMA.products AS p ON p.id = a.accessoryId
WHERE a.id IN (
        14
        ,15
        )

Note, you may have an error in your example: should WHERE accessories.id IN (14,15) be WHERE accessories.accessoryid IN (14,15)?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.