开发者

Select based on item within an array

I am looking for a mySQL command to select rows based on whether or not an ar开发者_如何学Pythonray field contains a specific item with that array.


Not quite sure whether you want to find an item in an array stored in the DB or you want to find all rows that match an item in an array.

For the former (given that MySQL doesn't support array types natively I'm assuming you're doing something to serialize the data into a string column or the like):

SELECT [columns] FROM [table] WHERE [array_column] LIKE "%{separator}[test_value]{separator}%"; 
e.g. SELECT * FROM my_table WHERE values_list LIKE "%|5|%";

will do the job (where separator is whatever character you're using to separate values stored into the string column - e.g. given an array [0, 5, 10, 15] and a separator "|" the serialized string would be "|0|5|10|15|")

For the second case:

SELECT [columns] FROM [table] WHERE [test_column] IN (list, of, some, values);
e.g. SELECT * FROM my_table WHERE my_value IN (0, 5, 10, 15);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜