Skip to content
Advertisement

Join two tables only if a value is null or a specific number

I have three tables in a database:

  • Product table – +100000 entries
  • Attribute table (list of possible attributes of a product)
  • Product attribtue table (which contains the value of the attribute of a product)

I am looking for 8 random products and one of their attributes (attribute_id = 2), but if a product hasn’t this attribute it should appear at the return of the query. I have been trying some sql queries without any succesful result because my return only shows the products that have the attribute and hide the others.

My three tables are like this:

And this is one of the queries I tried, I thought it was correct but it have the same problem as the others…

I was trying with left, inner and right join and nothing works.

Advertisement

Answer

You should put the condition for the left-joined table in the join, not the where clause

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