2013-05-23 18 views
5

Abbiamo prossimi tavoli nel nostro sistemaQuery per la corrispondenza di più righe?

Tabella oggetto

object_id object_description 
    1    "Car" 
    2    "Person" 

Tabella attributo

attribute_id attribute_name 
    1    "hair_color" 
    2    "height" 
    3    "number_of_doors" 
    4    "engine_size" 

Tabella valore_attributo

attribute_id attribute_value_id value 
    1    1    "black" 
    1    2    "blonde" 
    2    1    "more than 1 meter" 
    2    2    "less than 1 meter" 
    3    1    "5 doors" 
    3    2    "3 doors" 
    4    1    "more than 1.9" 
    4    2    "less than 1.9" 

Tabella object_attribute

object_id attribute_id attribute_value_id 
    1   3    1 -- Car, number of doors,5 
    1   3    2 -- Car, number of doors,2 
    1   4    1 -- Car, engine size, greater than 1.9 
    1   4    2 -- Car, engine size, less than 1.9 

Con questa struttura stiamo avendo un sacco di problemi per ottenere oggetti corrispondenti a più criteri (vale a dire ottenere tutte le auto con 3 porte e le dimensioni del motore più grande di 1.9) Attualmente stiamo usando interseca per fare questo

SELECT OBJECT_ID 
    FROM object_attribute 
WHERE attribute_id  = 3 
    AND attribute_value  = 2 
INTERSECT 
SELECT OBJECT_ID 
    FROM object_attribute 
WHERE attribute_id  = 4 
    AND attribute_value  = 1 

Ci sono oggetti diferent con il numero differenti di attributi, quindi non possiamo usare un numero fisso di join o INTERSECTs

C'è un modo per generare più combinazioni di tutti gli attributi in un "modo dinamico"?

Quello che vorremmo realizzare è una query dinamica che costruisce una vista come questa:

object_id | att_name_1 | att_value_1 | att_name_2 | att_value2 | att_name_n | attr_value_n

Poiché il numero di attributi è variabile dovremmo trigered e aggiornamento della query quando un nuovo oggetto viene inserito

Ragazzi penso che quello che ho in mente non è possibile, quindi ci sarà probabilmente andare con questa costruzione di query dinamica in fase di esecuzione. Grazie a tutti per le vostre risposte

+0

Potrebbe cross join oggetto su attributi e LEFT JOIN sui vostri risultati? O se vuoi solo attributi che esistono potresti iniziare da Object_Attribute e partecipare su entrambe le altre colonne? – Liath

+3

Quali RDBM? Retag per una migliore visibilità – Yaroslav

+2

È necessario creare una query in modo dinamico, con la giusta quantità di JOIN su di essi. – Borik

risposta

1

dopo alcune prove mi si avvicinò con la seguente query:

select distinct 
a.attribute_name, o.object_description, av.value, 
oa.attribute_id, oa.object_id, oa.attribute_value_id 
from object_attribute oa 
inner join attribute a on (oa.attribute_id = a.attribute_id and a.attribute_id = 3) 
inner join object o on (oa.object_id = o.object_id and o.object_id = 1) 
inner join attribute_value av on (oa.attribute_value_id = av.attribute_value_id and av.attribute_value_id = 2) 
where 
(av.attribute_id = 3 and o.object_id = 1 and av.attribute_value_id = 2) 

union 
select distinct 
a.attribute_name, o.object_description, av.value, 
oa.attribute_id, oa.object_id, oa.attribute_value_id 
from object_attribute oa 
inner join attribute a on (oa.attribute_id = a.attribute_id and a.attribute_id = 4) 
inner join object o on (oa.object_id = o.object_id and o.object_id = 1) 
inner join attribute_value av on (oa.attribute_value_id = av.attribute_value_id and  av.attribute_value_id = 1) 
where 
(av.attribute_id = 4 and o.object_id = 1 and av.attribute_value_id = 1) 

che si traduce nella seguente: query results

Se si utilizza MS SQL Server mi avrebbe messo in una stored procedure che accetta i tre Id come parametri.

+0

la tua query è buona ma non fornire un modo per ottenere quei campi dinamicamente. Voglio dire, stai facendo un joind con valori noti, ma vorrei costruire quella query usando una sorta di permutazione o combinazione dinamica. – Aitor

+0

bene se si utilizza MS SQL Server è possibile creare la query in modo dinamico e utilizzare sp_executesql per eseguirlo. http://msdn.microsoft.com/en-us/library/ms188001.aspx –

Problemi correlati