2.5. ��ѯһ����

Ҫ��һ�����м������ݾ�����ѯ����� SQL �� SELECT �����������;�ġ� ������Ϊѡ���б��г�Ҫ���ص��ֶβ��֣������б��г����м������ݵı�IJ��֣��� �Լ���ѡ�������������������ƵIJ��֣������磬Ҫ������ weather �������У����룺

SELECT * FROM weather;

���� * ��"�����ֶ�" ����д�� [1] ���ͬ���Ľ����������������ã�

SELECT city, temp_lo, temp_hi, prcp, date FROM weather;

�����Ӧ���ǣ�

     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      43 |      57 |    0 | 1994-11-29
 Hayward       |      37 |      54 |      | 1994-11-29
(3 rows)

�������ѡ���б���д������ʽ�������������ֶ��б����磬����ԣ�

SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;

����Ӧ�õó���

     city      | temp_avg |    date
---------------+----------+------------
 San Francisco |       48 | 1994-11-27
 San Francisco |       50 | 1994-11-29
 Hayward       |       45 | 1994-11-29
(3 rows)

��ע������� AS �Ӿ�����θ�����ֶ����������ġ���AS �Ӿ��ǿ�ѡ�ġ���

һ����ѯ����ʹ�� WHERE �Ӿ�"����"��������Ҫ��Щ�С� WHERE �Ӿ����һ����������ֵ�����ʽ��ֻ����Щ�������ʽΪ����вŻᱻ���ء� ��������������ʹ�ó��õIJ�����������AND��OR�� �� NOT���� ���磬����IJ�ѯ�����ɽ�ɽ���������������

SELECT * FROM weather
    WHERE city = 'San Francisco' AND prcp > 0.0;

�����

     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
(1 row)

�����Ҫ�󷵻صIJ�ѯ���ź���ģ�

SELECT * FROM weather
    ORDER BY city;

     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 Hayward       |      37 |      54 |      | 1994-11-29
 San Francisco |      43 |      57 |    0 | 1994-11-29
 San Francisco |      46 |      50 | 0.25 | 1994-11-27

���������������˳�򲢷Ǿ��������ģ��������ܿ��� San Francisco ����������� ���������ʹ���������䣬��ô�����ǻ�õ�����Ľ��

SELECT * FROM weather
    ORDER BY city, temp_lo;

�����Ҫ���ѯ�Ľ������ij��˳������ ���������ظ����������

SELECT DISTINCT city
    FROM weather;

     city
---------------
 Hayward
 San Francisco
(2 rows)

�ٴ�����������е�˳�����������ġ� ��������ʹ�� DISTINCT �� ORDER BY ����ȡһ�µĽ���� [2]

SELECT DISTINCT city
    FROM weather
    ORDER BY city;

Notes

[1]

��Ȼ SELECT * ���ڰѲ�ѯ�Ľ�������������ã� �����ձ���Ϊ���������������Ǻ����ķ����Ϊ��������һ���ֶξ͸ı��˽����

[2]

��һЩ���ݿ�ϵͳ������ϰ汾�� PostgreSQL�� DISTINCT ��ʵ���Զ����н���������� ORDER BY �Ƕ���ġ�������һ�㲢���� SQL ��׼��Ҫ�󣬲���Ŀǰ�� PostgreSQL ������֤ DISTINCT ���������б�����