1. 程式人生 > 實用技巧 >【優達學城測評】SQL-INSERT(9)

【優達學城測評】SQL-INSERT(9)

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

Insert into tablevalues(42,'stuff');

Insert into table (col2,col1)values('stuff',42)

112639_wtBT_2914586.jpg

insert into animalsvalues ('wibble','opossum','2014-11-14');

insert into animals (name,species,birthdate)values ('wibble','opossum','2014-11-14')


# Find the names of the individual animals that eat fish.

# The animals table has columns (name, species, birthdate) for each individual.
# The diet table has columns (species, food) for each food that a species eats.

QUERY = '''
select animals.name
from animals join diet on animals.species=diet.species
where food='fish'
'''

120640_bY25_2914586.jpg

another question:

Which species does the zoo ha

ve only one of?

wrong way:select species,count(*) as num from animals group by species where num=1;

REMEMBER :Thevalue of num come from count and group by.But where always runs before aggregations

121722_mQ4I_2914586.jpg

BECAUSE:There's no num column in the animals table.And you can't use where after a group by anyway.(而且你不能在 group by 後面使用where) If you tried this query out,you'd get a big error ,but if we change just one word,we can make this right,whereas where filters(篩選) the source table,animals,having filters the result table.So having applies after the group BI aggregation.

轉載於:https://my.oschina.net/Bettyty/blog/757874