The MySQL query below uses PHP to pull in the $sector, which is a single digit, and the $subsector_text, which is a comma separated string. The $subsector_text could be a single digit or a list of several IDs, such as "3,4,7,9".
$sql = "
SELECT DISTINCT a.id
, a.name
, a.category_id
, a.sector
, a.subsector
, a.year_in_operation
, a.state
, a.total_value
, b.country_id
, b.project_id
, c.isocode_3
, c.name
FROM com_barchan_project a
JOIN com_barchan_location b
ON b.project_id = a.id
JOIN com_barchan_country c
ON c.id = b.country_id
JOIN com_barchan_project_value_join d
ON a.id = d.project_id
WHERE a.state = 1
AND a.sector = '$sector'
AND a.subsector REGEXP '^{$subsector_text}[,]|[,]{$subsector_text}[,]|[,]{$subsector_text}$|^{$subsector_text}$'
ORDER
BY a.total_value DESC
, a.category_id ASC
, a.name ASC
";
The problem I'm having with the query above is with the line:
AND a.subsector REGEXP '^{$subsector_text}[,]|[,]{$subsector_text}[,]|[,]{$subsector_text}$|^{$subsector_text}$'
If the $subsector_text = "3,4,5,9", then it's only returning records that contain exactly "3,4,5,9" in the $subsector field.
The desired result is that it would return any record that has any of the values in the $subsector_text. For instance, all these should be returned, but are currently not. This list is an example and by no means exact.
1,3
1,5
1,3,7,9
3,5
3,4,5,9
9
3
5
4
How do I change the query to select any records that has a value in that's in the $subsector_text string?
Please NOTE: That if the $subsector_text = 11, then the following, as an example, should not be selected.
1
12
21
Any help would be greatly appreciated.