Skip to content
Advertisement

duplicate data being return with varied # of results in cross join

I created a database for a clothing brand. The products consists of 3 tables: products, product_photos, inventory.

The issue I’m having pertains to the amount of results in the product_photos, inventory tables that are returned. So say there should be 4 results in the inventory table (ex. size: s, m, l, xl AND quantity: 20, 20, 20, 20) and 1 result in the product_photos table (ex. photos: url). With my current query the product_photos data is duplicated 4 times (ex. photos: url, url, url, url).

My current query looks like this:

I have played around with some things such as changing the right’s to inner join’s and left join’s but right join seems to be the technical choice for what I’m trying to do.

Here is some sample data:

Advertisement

Answer

The reason for the many results is that both the product_photos as and the inventory table can have multiple records for the same product_id and so all pairwise combinations of records from both tables will be iterated (Cartesian product).

You can solve this by first selecting all records from both these tables with a union and then join that result with the products table:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement