I have one search box that I would like to have search 6 columns in my schools database when an input is made. So far the search box searches the name field only and returns a match for exact or partial inputs.
I would like to search for a specific city and have all results show up from the name AND city columns (instead of just the name column) and so on.
Say I wanted to search with a zip code, I would like the listings to be all schools in that zip code. And finally if I input 2 words (e.g. penn Philadelphia) I would like all penn schools to show that are in the name column AND city column only. (not just all the penns in the name or every school in Philadelphia) and so on. These may be an elementary questions on the matter but I've been searching for days with no success. Maybe better use of wildcards and the "AND" "OR" clauses would benefit me.
Can someone help me structure a sql query to accomplish this?
this is what I have so far:
SELECT * FROM schools
WHERE (name='%name%' OR address='%name%' OR city='%name%'OR zip='%name%')
SELECT * FROM schools WHERE name='penn' AND city = 'philadelphia';you can combine a few conditions together. Another approach:SELECT * FROM school s1, school s2 JOIN ON s1.name = s2.name WHERE s1.name = 'penn' AND s1.city = 'philadelphia';here you can also use multiple joins don't go wild with it but having a good index helps this latter robustSELECT * FROM schools WHERE ( SELECT * FROM schools WHERE name = 'penn') AS s1 AND city = 'philadelphia';. Don't go too deep on this as it would get nasty. Choose what fits you best=and%do not combine like I think you intend. See also stackoverflow.com/questions/1127088/mysql-like-in