查询表
我们可以用通常的关系查询选择和影射查询检索 weather 表.一个
SQL
select 语句可以用于此事. 该语句分成目标列表(返回的字段)部分和条件(限制条件)部分.例如,我们检索weather表中的所有行,键入:
SELECT * FROM weather;
输出为:
+--------------+---------+---------+------+------------+
|city | temp_lo | temp_hi | prcp | date |
+--------------+---------+---------+------+------------+
|San Francisco | 46 | 50 | 0.25 | 11-27-1994 |
+--------------+---------+---------+------+------------+
|San Francisco | 43 | 57 | 0 | 11-29-1994 |
+--------------+---------+---------+------+------------+
|Hayward | 37 | 54 | | 11-29-1994 |
+--------------+---------+---------+------+------------+
你可以在目标列表中输入任意的表达式.例如:
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
在任何查询条件中条件都可以使用任意布尔操作符(
and,
or 和
not)
例如,
SELECT * FROM weather
WHERE city = 'San Francisco'
AND prcp > 0.0;
结果是:
+--------------+---------+---------+------+------------+
|city | temp_lo | temp_hi | prcp | date |
+--------------+---------+---------+------+------------+
|San Francisco | 46 | 50 | 0.25 | 11-27-1994 |
+--------------+---------+---------+------+------------+
最后,你可以对 select 的结果
排序输出或
去除重复记录.
SELECT DISTINCT city
FROM weather
ORDER BY city;