Che è una dichiarazione di SQL per selezionare un elemento che ha diversi attributi in un elenco di elementi / attributi?
-
06-09-2019 - |
Domanda
Dire che ho una tabella che ha gli elementi e gli attributi elencati come,
frog green
cat furry
frog nice
cat 4 legs
frog 4 legs
Dalla colonna che voglio per selezionare gli oggetti unici che hanno sia l'attributo verde e 4 gambe. Mi aspetto di tornare appena l'oggetto rana in questo caso. Qual è la query più efficace per fare questo?
Soluzione
select item.name
from item
where item.attribute in ('4 legs', 'green')
group by item.name
having count(distinct item.attribute) = 2
Altri suggerimenti
Il modo più efficace per farlo è con un self-join:
SELECT * FROM attributes a1
JOIN attributes a2 USING (item_name) -- e.g. frog
WHERE a1.value = 'green' AND a2.value = '4 legs';
Un'altra soluzione che alcune persone usano è un trucco con GROUP BY:
SELECT item_name FROM attributes
WHERE value IN ('4 legs', 'green')
GROUP BY item_name
HAVING COUNT(*) = 2;
Ma la GROUP BY soluzione potrebbe non essere efficiente come un JOIN, a seconda di quale marca di RDBMS che si usa. Anche un metodo può scalare meglio come il volume nella tabella cresce.
select * from tabella in cui cosa = 'rana'
niente è meglio sapere esattamente quello che vuoi.
select
item, count(*)
from
@temp
where
attribute in ('4 legs','green')
group by
item
having
count(*) = 2 -- this "2" needs to be replaced with however many attributes you have
Si potrebbe anche interrogare ogni attributo separatamente, e poi li si intersecano ...
/*
-- create sample table...
create table #temp1
(item varchar(max),
attrib varchar(max))
-- populate sample table (SQL 08)...
insert #temp1
values ('frog', 'green'), ('cat', 'furry'), ('frog', 'nice'), ('cat', '4 legs'), ('frog', '4 legs')
*/
SELECT item
FROM #temp1
WHERE attrib = 'green'
INTERSECT
SELECT item
FROM #temp1
WHERE attrib = '4 legs'
creare due tabelle, una delle voci e uno degli attributi.
Gli articoli hanno potuto essere il nome, intAttributeID, dove intAttributeID è un riferimento chiave esterna alla tabella attributi. In questo modo si può fare una dichiarazione prescelta basato fuori tutto ciò che ti interessa.
Ma forse questo può aiutare:
SELECT *
FROM tbl t1
INNER JOIN tbl t2 ON t1.Name = t2.Name
WHERE t1.Attribute = 'green' AND t2.Attribute = '4 legs'
Hard perché non è un modello normalizzato. E 'un week-end.
Stai filtrando su più, le righe non connessi, in modo che avrebbe dovuto estrarre ogni attributo a turno e poi abbinare oggetti.
SELECT
item
FROM
(SELECT
item
FROM
Mytable
WHERE
attribute = '4 legs') k1
JOIN
(SELECT
item
FROM
Mytable
WHERE
attribute = 'green') k2 ON k1.item = k2.item
Se possibile, vorrei ridisegnare. Questo non è qualcosa che si potrà mai essere in grado di interrogare in modo efficace 12 valori sulla allo stesso tempo su (richiederà 12 si unisce)
Si prega di leggere questo articolo di Wikipedia http://en.wikipedia.org/wiki/Entity-Attribute-Value_model# Svantaggi
Non ancora visto un database che ha utilizzato questo modello che non incorrere in gravi problemi di prestazioni alla fine. Questo design appare elegante per le persone non-database, ma in realtà è di solito un segno di un database mal progettato.