Hi there
I’ve been struggling with getting “OR” searches to work on a site I’m developing. I’ve tried SuperSearch, Low Search and of course the channel:entries tag. In the end I’ve realised that it’s an SQL query that’s gonna solve my problems.
What I have is *nearly* working, but not quite.
In plain english, what I’m trying to achieve is this:
- The user is searching a job vacancy site using the ‘subject’ and ‘region’ fields.
- Each job vacancy can have 2 ‘subjects’ (eg, ‘Business Studies’ and ‘Economics’ could be 1 teaching job). These subjects are 2 custom fields.
- They also search by ‘region’. For this I’m using categories, so a possible value could be ‘80’.
- Also in the mix is the status of the vacancy. It must be either ‘Published’ or ‘School View’
Here’s the sql I have that isn’t really working:
SELECT ct.entry_id, ct.title, cp.cat_id
FROM exp_channel_titles ct
LEFT JOIN exp_category_posts cp ON ct.entry_id = cp.entry_id
LEFT JOIN exp_channel_data cd ON cp.entry_id = cd.entry_id
WHERE cp.cat_id = '$region'
AND cd.field_id_182 = '$subject'
OR cd.field_id_220 = '$subject'
AND ct.channel_id = 4
AND ct.status = 'Published'
OR ct.status = 'School View'
ORDER BY cd.field_id_134 ASC";$region and $subject are php variables.
Can anyone see anything obviously wrong with this? I know that some of the ‘AND’s are being ignore but no idea why.
Cheers
Jim