2015-10-22 12 views
8

Ho una tabella con una colonna varcharcategoryIds. Esso contiene alcuni ID separati da virgole, ad esempio:Come posso verificare se un numero è contenuto in una lista separata da virgole memorizzata in una colonna varchar?

id  categoryIds 
-------------------- 
1  3,7,12,33,43 

voglio fare una dichiarazione prescelta e verificare se un int esiste in quella colonna. Qualcosa di simile a questo:

select * 
from myTable 
where 3 in (categoryIds) 

So che questo è possibile in MySQL facendo this, ma si può fare in SQL Server come bene?

Ho provato gettando l'int a un char, che gestisce la seguente dichiarazione:

select * 
from myTable 
where '3' in (categoryIds) 

Ma non sembra che ci sia alcun "out of the box" supporto per i separati da virgola liste come si ritorna Niente.

+4

Dovresti pensare al tuo design di db. Memorizzare i valori in formato csv non è una buona pratica. – Jens

+3

Non memorizzare i dati come valori separati da virgola nella soluzione. Un valore per riga è il modo SQL! – jarlh

+1

Forse qualcosa di simile: 'SELECT * FROM WHERE myTable '' + categoryId + '' LIKE '%, 3,%';' – dwjv

risposta

9

Si dovrebbe riprogettare questa tabella per suddividere tali valori vengano separati da virgola ad essere in singole righe. Tuttavia, se ciò non fosse possibile, si sono bloccati con il fare il confronto tra stringhe invece:

DECLARE @id INT = 3 
DECLARE @stringId VARCHAR(50) = CAST(@id AS VARCHAR(50)) 

SELECT * 
FROM MyTable 
WHERE categoryIds = @stringId -- When there is only 1 id in the table 
OR categoryIds LIKE @stringId + ',%' -- When the id is the first one 
OR categoryIds LIKE '%,' + @stringId + ',%' -- When the id is in the middle 
OR categoryIds LIKE '%,' + @stringId -- When the id is at the end 
+0

Se voglio cercare id = 2 (due), recupererà 12, 20, 21 ecc. È corretto? –

+0

@NaveenNikhil In realtà no, apparirà solo per numeri interi specifici, ecco perché ci sono 4 clausole 'WHERE' distinte qui. – DavidG

+0

Ma queste clausole sono combinate con OR. Puoi confermare? –

0

Si potrebbe utilizzare SQL dinamico come questo:

DECLARE  @categoryIds nvarchar(50) = '1, 2, 3, 4, 5' 

EXEC  ('SELECT  * 
       FROM  myTable 
       WHERE  categoryId IN (' + @categoryIds + ')') 
0

Non so se questo sarebbe più veloce o più lento rispetto DavidG di suggerimento, ma al fine di ottenere gli stessi risultati con un solo controllo, si può fare:

DECLARE @categoryId INT 
SET @categoryId = 3 

SELECT * 
FROM myTable 
WHERE CHARINDEX(',' + CAST(@categoryId AS VARCHAR(MAX)) + ',', ',' + categoryIds + ',') > 0 
-4

uso funzione FIND_IN_SET() mysql

Sintassi

SELECT * FROM <table name> as a WHERE FIND_IN_SET(value to search in string,comma separated string); 

Esempio

SELECT * FROM <table name> as a WHERE FIND_IN_SET(5,"1,2,3,4,5,6"); 
+4

Questa domanda non era indirizzata a MySQL, FIND_IN_SET non è disponibile in SQL Server. –

2
SELECT * 
FROM myTable 
WHERE (',' + RTRIM(categoryIds) + ',') LIKE '%,' + @stringId + ',%' 

Ecco @stringId è il testo da ricercare. In questo modo è possibile evitare inutili multiple condizioni in cui

Cordiali saluti, Raghu.M.

Problemi correlati