Hi,
I have a property database and one of the fields contains a comma separated list/string which are property types; this field data looks like: 1,2,3,4,5,6
What I need to do is to query the database and ask whether an ID (a variable) (property type) exists in that string. In plain english I’d say: is “2” in “1,2,3,4,5,6”?
I’ve tried using “IN” but it doesn’t work as it’s the database field that contains the comma separated string and the ID is the external variable/query - i.e. I’ve seem examples of an IN statement like this:
(in all of these examples I’ll use “2” as an example of a query variable)
SELECT * FROM property_tbl
-> WHERE property_type IN (1,2,3,4,5,6);
but in my case I need it to be the other way around:
SELECT * FROM property_tbl
-> WHERE 2 IN (property_type);
(where a value of property_type might be “1,2,3,4,5,6”)
At the moment I’m using an ugly method to get this working that looks like this:
SELECT * FROM property_tbl
-> WHERE (property_type LIKE (‘2’) OR property_type LIKE (‘2%’) OR property_type LIKE (‘%2’) OR property_type LIKE (‘%2%’))
this works but I figured there must be a better way of doing it.
Can anyone help.
Many thanks.