1. Trang chủ
  2. » Công Nghệ Thông Tin

The Language of SQL- P21 ppsx

5 210 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 88,18 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The percent % before LOVE means that you will accept a phrase with any char-acters before LOVE.. An equivalent statement in Oracle is: SELECT MovieTitle AS Movie FROM Movies WHERE MovieT

Trang 1

In this example, the percent (%) symbol is used as a wildcard The percent (%)

wildcard means any characters It can also represent a list of zero characters The

percent (%) before LOVE means that you will accept a phrase with any char-acters before LOVE Similarly, the percent (%) after LOVE means that you will accept a phrase with any characters after LOVE

D A T A B A S E D I F F E R E N C E S : O r a c l e

Unlike Microsoft SQL Server and MySQL, Oracle is case sensitive when determining matches for literal values In Oracle, LOVE is not the same as Love An equivalent statement in Oracle is: SELECT

MovieTitle AS Movie

FROM Movies

WHERE MovieTitle LIKE '%Love%';

A better solution in Oracle is to use the UPPER function to convert your data to uppercase, as follows: SELECT

MovieTitle AS Movie

FROM Movies

WHERE UPPER (MovieTitle) LIKE '%LOVE%';

In other words, you are looking for any movie title that contains the phrase LOVE Here is the data returned from the previousSELECT:

Movie

Love Actually

Love and Death

Everyone Says I Love You

Down with Love

1 Love Actually

2 His Girl Friday

3 Love and Death

4 Sweet and Lowdown

5 Everyone Says I Love You

6 Down with Love

7 101 Dalmatians

Chapter 9 ■ Inexact Matches

86

Trang 2

Notice that LOVE appears as the first word, the last word, and sometimes in the

middle of the movie title

Let’s now attempt to find only movies that begin with LOVE If you issue:

SELECT

MovieTitle AS 'Movie'

FROM Movies

WHERE MovieTitle LIKE 'LOVE%'

you will only retrieve this data:

Movie

Love Actually

Love and Death

Since you are now specifying the percent (%) wildcard after the phrase LOVE,

you will only get back movies that begin with LOVE

Similarly, if you issue:

SELECT

MovieTitle AS 'Movie'

FROM Movies

WHERE MovieTitle LIKE '%LOVE'

you only get this data:

Movie

Down with Love

This is because you now specified that the phrase must end with the phrase LOVE.

What if you only want to see movies that contain the word LOVE in the middle

of the title, but you don’t want to see movies where it is at the beginning or end?

The solution is to specify:

SELECT

MovieTitle AS 'Movie'

FROM Movies

WHERE MovieTitle LIKE '% LOVE %'

Trang 3

Notice that a space has been inserted between the phrase LOVE and the percent (%) wildcards on either side This ensures that there is at least one space on either side of the word The data brought back from this statement is:

Movie

Everyone Says I Love You

Wildcards

The percent (%) symbol is the most common wildcard used with the LIKE

operator However, there are a few other possibilities These include the

under-score character (_), a characterlist enclosed in square brackets, and a caret symbol (^) plus a characterlist enclosed in square brackets The following table lists these

wildcards and their meanings:

% any characters (can be zero characters)

_ exactly one character (can be any character)

[characterlist ] exactly one character in the character list

[^characterlist ] exactly one character not in the character set

We’re going to use the following Actors table to illustrate statements for the remainder of this chapter:

1 Cary Grant

2 Mary Steenburgen

4 Dustin Hoffman

6 Gary Cooper

7 Julie Andrews

Chapter 9 ■ Inexact Matches

88

Trang 4

Here’s an illustration of how the underscore (_) wildcard character can be used:

SELECT

FirstName,

LastName

FROM Actors

WHERE FirstName LIKE '_ARY'

The output of thisSELECTis:

Cary Grant

Mary Steenburgen

Gary Cooper

This statement retrieves these three actors because all have a first name

consist-ing of exactly one character, followed by the phrase ARY

Likewise, if you issue this statement:

SELECT

FirstName,

LastName

FROM Actors

WHERE FirstName LIKE 'J_N'

it produces:

Jon Voight

The actor John Wayne is not selected since John doesn’t fit the J_N pattern An

underscore can only stand for one character

The final wildcards we’ll discuss, [characterlist] and [^characterlist], enable you

to specify multiple wildcard values in a single position

D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e

The [ characterlist ] and [^ characterlist ] wildcards are not available in MySQL or Oracle.

Trang 5

The following illustrates the [characterlist] wildcard:

SELECT

FirstName,

LastName

FROM Actors

WHERE FirstName LIKE '[CM]ARY'

This retrieves any rows where FirstName begins with a C or M and ends with ARY The result is:

Cary Grant

Mary Steenburgen

The following illustrates the [^characterlist] wildcard:

SELECT

FirstName,

LastName

FROM Actors

WHERE FirstName LIKE '[^CG]ARY'

This selects any rows where FirstName does not begin with a C or G and ends

with ARY The result is:

Mary Steenburgen

Finally, it should be noted that theNOToperator can be combined withLIKE, as

in this example:

SELECT

FirstName,

LastName

FROM Actors

WHERE FirstName LIKE '%ARY%'

AND FirstName NOT LIKE '[MG]ARY'

Chapter 9 ■ Inexact Matches

90

Ngày đăng: 05/07/2014, 05:20