习题七

          Current tables:

robots

id

name

1

Robot 2000

2

Champion Robot 2001

3

Dragon

4

Turbo Robot 2002

5

Super Robot 2003

6

Super Turbo Robot 2004

7

Not A Robot

8

Unreleased Turbo Robot 2111

           习题二十八、LIKE

use the LIKE command in order to search through text-based values. With LIKE, there are two special characters: % and _.

The percent sign (%) represents zero, one, or multiplecharacters. 

The underscore (
_)represents one character. 

SELECT * FROM robots

WHERE name LIKE "%Robot%"; 

would yield all values that contain "Robot" inthe name.

Result:

id

name

1

Robot 2000

2

Champion Robot 2001

4

Turbo Robot 2002

5

Super Robot 2003

6

Super Turbo Robot 2004

7

Not A Robot

8

Unreleased Turbo Robot 2111

run a query that returns "Robot" followed by ayear between 2000 and 2099? (So 2015 is a valid value at the end, but 2123 isnot.) 

select * from robots

where robots.name like "%Robot 20__" ;

Result:

id

name

1

Robot 2000

2

Champion Robot 2001

4

Turbo Robot 2002

5

Super Robot 2003

6

Super Turbo Robot 2004

 Current tables:

friends_of_pickles

id

name

gender

species

height_cm

1

Dave

male

human

180

2

Mary

female

human

160

3

Fry

male

cat

30

4

Leela

female

cat

25

5

Odie

male

dog

40

6

Jumpy

male

dog

35

7

Sneakers

male

dog

55

            习题二十八、CASE

use a CASE statement to return certain values when certain scenariosare true. 

A 
CASE statement takes the following form: 

CASE WHEN *first thing is true* THEN *value1*
          WHEN *second thing is true*THEN *value2*
          ...
         ELSE *value for all othersituations* 
END 

 in order to return the number of legs foreach row in friends_of_pickles: 
SELECT *, 
           CASE WHEN species = 'human'THEN 2

 ELSE 4

END ASnum_legs 

FROM friends_of_pickles;

Result:

id

name

gender

species

height_cm

num_legs

1

Dave

male

human

180

2

2

Mary

female

human

160

2

3

Fry

male

cat

30

4

4

Leela

female

cat

25

4

5

Odie

male

dog

40

4

6

Jumpy

male

dog

35

4

7

Sneakers

male

dog

55

4


return the results with a column named sound that returns "talk" forhumans, "bark" for dogs, and "meow" for cats?

select * ,

case when species = 'human' then 'talk'

when species = 'dog' then 'bark'

when species = 'cat'  then 'meow'

end

as sound

from friends_of_pickles ;

Result:

id

name

gender

species

height_cm

sound

1

Dave

male

human

180

talk

2

Mary

female

human

160

talk

3

Fry

male

cat

30

meow

4

Leela

female

cat

25

meow

5

Odie

male

dog

40

bark

6

Jumpy

male

dog

35

bark

7

Sneakers

male

dog

55

bark

          Current tables:

robots

id

name

location

1

R2000 - Robot 2000

New City, NY

2

R2001 - Champion Robot 2001

Palo Alto, CA

3

D0001 - Dragon

New York City, NY

4

R2002 - Turbo Robot 2002

Spring Valley, NY

5

R2003 - Super Robot 2003

Nyack, NY

6

R2004 - Super Turbo Robot 2004

Tampa, FL

7

N0001 - Not A Robot

Seattle, WA

8

U2111 - Unreleased Turbo Robot 2111

Buffalo, NY

          习题二十九、SUBSTR

SUBSTR is used in this format: SUBSTR(column_name, index,number_of_characters) 

index is a number that denotes where you would start thesubstring. 1 would indicate the first character, 2 would indicated the secondcharacter, etc. The index could also be negative, which means you would countfrom the end of the string. -1 would denote the last character, -2 would denotethe 2nd to last character, etc.

number_of_characters is optional; if it is not included, thesubstring contains the "rest of the string". 

Here are some examples:
SUBSTR(name, 1, 5) is the first 5 characters of the name. 
SUBSTR(name, -4) is the last 4 characters of the name. 

SELECT * FROM robots

WHERE SUBSTR(name, -4) LIKE '20__'; 

is another way of returning all of the robotsthat have been released between 2000 and 2099.

Result:

id

name

location

1

R2000 - Robot 2000

New City, NY

2

R2001 - Champion Robot 2001

Palo Alto, CA

4

R2002 - Turbo Robot 2002

Spring Valley, NY

5

R2003 - Super Robot 2003

Nyack, NY

6

R2004 - Super Turbo Robot 2004

Tampa, FL

Note: In other versions of SQL, you could use RIGHT to do this.

return all of the robots that are located inNY?

SELECT *from robots

where substr (location,-2) like 'NY' ;

Result:

id

name

location

1

R2000 - Robot 2000

New City, NY

3

D0001 - Dragon

New York City, NY

4

R2002 - Turbo Robot 2002

Spring Valley, NY

5

R2003 - Super Robot 2003

Nyack, NY

8

U2111 - Unreleased Turbo Robot 2111

Buffalo, NY

         Current tables:

fighters

id

name

gun

sword

tank

1

US Marine

Colt 9mm SMG

Swiss Army Knife

M1A1 Abrams Tank

2

John Wilkes Booth

.44 caliber Derringer

null

null

3

Zorro

null

Sword of Zorro

null

4

Innocent Bystander

null

null

null

         习题三十、CIALESCE

COALESCE takes a list of columns, and returns thevalue of the first column that is not null. 

wanted to find the most powerful weapon that a combatanthas on hand. If value of gun is not null, that is the valuereturned. Otherwise, the value of sword is returned. Then you would run: 
SELECT name, COALESCE(gun, sword)

 as weapon

FROM fighters; 

Result:

name

weapon

US Marine

Colt 9mm SMG

John Wilkes Booth

.44 caliber Derringer

Zorro

Sword of Zorro

Innocent Bystander

null


a fighter's tank could count as a weapon, and it would takeprecedence over the gun and the sword. Could you find each fighter's weapon inthat scenario?

select name ,coalesce(tank,gun,sword)

          as weapon

         from fighters;

Result:

name

weapon

US Marine

M1A1 Abrams Tank

John Wilkes Booth

.44 caliber Derringer

Zorro

Sword of Zorro

Innocent Bystander

null




你可能感兴趣的:(SQL基础学习)