mrbone / mrbone.github.io

0 stars 0 forks source link

sql入门 #77

Closed mrbone closed 6 years ago

mrbone commented 6 years ago

[TOC]

数据类型

String

number

date

select

select columns from tables where condition ;

示例:

other examle

select name from animals where (species = 'llama') and (birthdate <= '1998-12-31') and (birthdate >= '1995-1-1')

其他限定

以下是官方解释

以下是上个视频中介绍的新 select 子句:
... limit count 只返回结果表格的前 count 行。
... limit count offset skip 返回前 skip 行之后的 count 行。
... order by columns ... order by columns desc 使用 columns(一列或多列,用逗号分隔)作为排序键对列排序。数字列将按数字顺序排序;字符串列将按字母顺序排序。desc 表示顺序为逆序(desc-结尾的排序)。
... group by columns 更改集合的行为,例如 max、count 和 sum。对于 group by,集合将为 columns 中的每个唯一值返回一行。

示例:找出 animals 数据库中的所有 species 并按照从多到少排序

insert

insert into animals (name, species, birthdate) values('michy', 'opossum', '2018-01-24');

join

这两种方案完全等效,但原理稍有不同。

having

寻找只有一种 species 的列

select species, count(*) as num from animals group by species where num = 1 # wrong!!!
select species, count(*) as num from animals group by species where num = 1 # write

wherehaving 的区别在

示例:
找出动物园中只有一个动物吃的食物。

select food, count(animals.name) as num
    from animals join diet on animals.species = diet.species
    group by food
    having num = 1
select food, count(animals.name) as num
    from animals, diet
    where animals.species = diet.species
    group by food
    having num = 1
mrbone commented 6 years ago

65