I have following query which is used to autocomplete school names that user is searching. If $query = har
SELECT *,
CASE
WHEN text LIKE '$query' THEN 1
WHEN text LIKE '$query%' THEN 2
WHEN text LIKE '%$query%' THEN 3
end AS priority
FROM (SELECT b.school_name AS `text`,
'school' AS `type`,
b.slug AS `id`,
n.neighbourhood AS 'params'
FROM schools b
LEFT JOIN school_addresses ba
ON ( b.id = ba.school_id )
LEFT JOIN neighbourhoods n
ON ( ba.neighbourhood_id = n.id )
WHERE b.entity_status = 'active'
AND ba.city_id = '$city'
AND b.visibility != 'delisted'
AND (b.school_name LIKE '$query'
OR b.school_name LIKE '$query%'
OR b.school_name LIKE '%$query%')
UNION
SELECT tg.OPTION AS `text`,
'tags' AS `type`,
tg.OPTION AS `id`,
tg.OPTION AS 'params'
FROM tags t
LEFT JOIN tag_options tg
ON ( t.id = tg.tag_id )
WHERE t.tag = 'Cuisines'
AND (tg.OPTION LIKE '$query'
OR tg.OPTION LIKE '$query%'
OR tg.OPTION LIKE '%$query%')
UNION
SELECT category AS `text`,
'category' AS `type`,
category AS `id`,
category AS 'params'
FROM categories
WHERE category LIKE '$query'
OR category LIKE '$query%'
OR category LIKE '%$query%'
UNION
SELECT area AS `text`,
'area' AS `type`,
id AS `id`,
id AS 'params'
FROM areas
WHERE city_id = '$city'
AND (area LIKE '$query'
OR area LIKE '$query%'
OR area LIKE '%$query%')
UNION
SELECT district AS `text`,
'districts' AS `type`,
id AS `id`,
id AS 'params'
FROM districts
WHERE city_id = '$city'
AND (district LIKE '$query'
OR district LIKE '$query%'
OR district LIKE '%$query%')
UNION
SELECT neighbourhood AS `text`,
'neighbourhood' AS `type`,
id AS `id`,
id AS 'params'
FROM neighbourhoods
WHERE city_id = '$city'
AND (neighbourhood LIKE '$query'
OR neighbourhood LIKE '$query%'
OR neighbourhood LIKE '%$query%')
) AS t1
WHERE 1
ORDER BY priority
LIMIT 5
This is the result it yields
'text' 'type' 'id' 'params' 'priority'
Harvard mba harv-ny-city new york 2
Harcum mba har-pa Pa 2
Harford mba harf-md Maryland 2
My question is how can i search using both "Name of the school" 'text' in above query and "Place of the school" 'params' in above query. Like if $query = 'harford ma' Then it should yield results like this:
'text' 'type' 'id' 'params' 'priority'
Harford mba harf-md Maryland 2
Harford mba harv-ny-city new york 2
Harford mba har-pa Pa 2
I been playing with this for almost whole day now, with no results.
Logic-> This is an auto search functionality in my site. User can try to look for school names or cities of those schools. But user can also search both also. For example there is School iit in bombay, delhi, chennai. User can search like: "iit de" -> as soon as user type this it should auto complete and bring in iit Delhi at top search, then other iit locations. In total it should show 5 results.