I need to perform a query like this:
Assume the columns of a table like - <key>,<开发者_C百科name>,<value>
and I need to find the value for a name = 'NAME' under the following conditions.
- The key should be in keys_i where name_i = 'NAME_i' and value = 'VALUE_i'.
- The key should be in keys_j where name_j = 'NAME_j' and value = 'VALUE_j'.
- The key should be in keys_k where name_k = 'NAME_k' and value = 'VALUE_k'.
I am currently using a nested query of the form:
Select a.value from <table_name> a
where a.name='NAME'
and a.key IN(
Select b.key
from <table_name> b
where (b.name = 'NAME_i' and b.value = 'VALUE_i')
and b.key IN(
Select c.key
from <table_name> c
where (c.name = 'NAME_j'
and c.value = 'VALUE_j')
and c.key IN(
Select d.key
from <table_name> d
where (d.name = 'NAME_k' and d.value = 'VALUE_k'))));
Is there a more generalized form using Self Join or any other technique? My table is small and optimization isn't really required but what would the ideal solution for large data sets?
I think this heavily depends on the database implementation and how query optimizer will rewrite each query type. Don't think you can answer this question at once about all database systems.
My case is for PL/SQL query and Oracle DB. Would also help to know for MySQL.
精彩评论