Skip to content
Advertisement

Find common value from a column where value from separate column matches 3 different values

Apologies for the poorly worded title. I was not entirely sure how to describe the issue. It’s easier illustrated too me.

I have a database table that lists content ids and values. One ID can have different values so id = 1 may have 1 or many rows depending on how many values are associated with it.

I want to find all ids that are associated with 3 different values in this case. Currently, the SQL is written in several subqueries but this causes a great deal of time to resolve if there are a lot of different values being searched for.

example:

The content can have many linked tables to do subquery searches on. The more added the more run time which is not ideal. What is the best method to reducing the number of subqueries?

Advertisement

Answer

you could use inner join with table2 using different table name alias

and for better performance you should add a composite index on table2 on column (value, id) and on table table1 on columns ( type, id)

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