SELECT语句用来从数据表中检索信息。语句的一般格式是:
1.查询所有数据
2.查询特殊行
3.查询唯一记录使用关键字DISTINCT
4.排序
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;
1.查询所有数据
select * from pet;
2.查询特殊行
SELECT * FROM pet WHERE name = 'Bowser';
SELECT * FROM pet WHERE birth > '1998-1-1';
SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
AND和OR可以混用,但AND比OR具有更高的优先级。如果你使用两个操作符,使用圆括号指明如何对条件进行分组
SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm') OR (species = 'dog' AND sex = 'f');
3.查询唯一记录使用关键字DISTINCT
SELECT DISTINCT owner FROM pet;
4.排序
顺序:SELECT name, birth FROM pet ORDER BY birth;
逆序:SELECT name, birth FROM pet ORDER BY birth desc;