Database Reference
In-Depth Information
Figure 6.8
Use LIKE to fuzzy match.
You will have noticed that we did not use the asterisk as the match all characters as you
may be familiar with from other languages. SQL uses the percent sign (%) to signify that
any number of characters can be matched. Putting the percent sign at the end of the string
we are looking for makes SQL match anything that begins with the string. To match any-
thing with Home at the end we would use:
SELECT *
FROM
webpage
WHERE
Title LIKE “%Home”
And if we were looking for a string with the word Home somewhere in it, we would put
the percent sign at both ends of the string as follows:
SELECT *
FROM
webpage
WHERE
Title LIKE “%Home%”
Before you run the query above, try and work out what it will select from our table. Why
do you think it works the way it does?
As well as using the percent sign to match multiple characters, you can also use the
underscore (_) to match single characters as follows:
SELECT *
FROM
log
WHERE
browser LIKE “Mozilla/_._%”
This will match any version of the Mozilla browser, no matter what browser description
follows.
SELECT AS (aliases)
On occasions, you may need to select the same column twice in a query. An example of an
application of this will occur in the chapter on aggregate functions. The simplest way to
select a column twice is by quoting it twice in the column list as follows:
Search WWH ::




Custom Search