Reputation: 23
Let's consider the following table as a toy example:
id / to / status 1 / steve,jimmy / 0,2 2 / john,julia / 1,2 3 / julia,peggy / 0,1
I want to get the rows where "Julia" is in "to" and where her corresponding "status" is "0".
In this easy example, that is row n°3.
How can I write such a query in SQL?
P.S.: I am using SQLite.
Upvotes: 2
Views: 287
Reputation: 50970
SQLite contains no built in functions to manipulate comma-separated lists that are embedded in single columns. Other databases do, but even they will have some trouble with your requirement, which is to obtain a value from one comma-separated string correlated to the index of another value in a different comma-separated string.
To do this, you will have to write a user-defined SQLite function that takes two column values and two string values, obtains the index of the first value in the first column, extracts the value from the same index in the second column, and compares that value with the second specified value.
In general, comma-separated strings like this are a no-no in relational databases and, honestly, I wouldn't even try to solve this problem in SQL. If normalizing the database structure is not an option, just do it in code.
Upvotes: 5