1- 查询整张表的所有数据:
SELECT * FROM myfirst_schema.pets;
如果当前表的数据比较多,只想看到特定行的数据:
select * from pets where name='shagou';
在上面这个查询语句中,可以制定任何列为条件:
select species from pets where birthday>='2018-10-31' and name='shagou';
这里是用了一个逻辑运算符‘and’,还有一个‘or’运算符
select species from pets where birthday>='2018-10-31' or name='shagou';
‘and’ 和 'or’可以混合使用,但是and优先级高于or,如果你像同时使用这两个运算符,最好使用括号明确指出如何分组
select owner from pets where (name='shagou' and sex='m')or(name='phili'and sex='f');
2- 排序行
对表数据按照某种有意义的方式排序,通常更容易检查查询结果。要对结果进行排序使用ORDER BY 子句。这种排序方式默认是升序,若要反向排序,则需要加关键字DESC
查询结果按照升序:select name, birthday from pets order by birthday;
降序: select name, birthday from pets order by birthday DESC;
也可以对多个列进行排序,并且可以按不同方向对不同的列及逆行排序
例如,要按动物类型按升序排序,然后按动物类型中的出生日期按降序排序(最年轻的动物首先):select name, species,birthday from pets order by species,birthday desc;
该DESC关键字仅适用于紧邻其前面的列名(birthday); 它不会影响species列排序顺序。`