Mysql
 SQL >> Database >  >> RDS >> Mysql

Cercare valori specifici in una colonna in stile CSV su MySQL

Prima di tutto, un gentile promemoria: un database ben progettato non dovrebbe mai memorizzare valori separati da virgola in un'unica colonna, e probabilmente lo sai già. Se la tabella fosse normalizzata, recuperare gli elementi che vuoi sarebbe immediato:

select distinct i.Itemid from Item i inner join ItemFeature f on f.ItemId = i.ItemId where f.Feature in ('AB', 'PQ') 

Puoi comunque abbinare il testo separato da virgole, benché sia tutt'altro che efficiente:

select Id from Item where instr(concat(',', Features, ','), ',AB,') <> 0 or instr(concat(',', Features, ','), ',PQ,') <> 0