Changing queries dynamically
But what if the user does not want to submit a precise query but needs a list of the possibilities? There are a couple of ways to clarify the search. We could first keep a list of the common search queries. This is something done often by the likes of Google and Yahoo!. This works very well with large datasets served through web servers because it uses a static list of terms and simply culls them out. For more dedicated applications, one can use MySQL's pattern matching ability to present known options on-the-fly.
Pattern matching in MySQL queries
Where Python's regular expression engine is very robust, MySQL supports the two following metacharacters for forming regular expressions:
%
: Zero or more characters matched in aggregate_
: Any single character matched individually
Pattern matching is always a matter of comparison. Therefore, with either of these, never use operators of equality.
SELECT * FROM menu WHERE name = 's%'; WRONG SELECT * FROM menu WHERE...