2.6. �ڱ�֮������

��ĿǰΪֹ�����ǵIJ�ѯһ��ֻ������һ���� ��ѯ����һ�η��ʶ����������ij�ַ�ʽ����һ������ͬʱ����ñ�Ķ���С� һ��ͬʱ����ͬһ�����߲�ͬ��Ķ���еIJ�ѯ�����ӣ�join����ѯ�� ������˵�����������г�����������¼�Լ���Щ��¼��صij��С� Ҫʵ�����Ŀ�꣬������Ҫ�� weather��ÿ�е�city �ֶκ�cities�������е�name�ֶν��бȽϣ� ��ѡȡ��Щ��Щ��ֵ��ƥ����С�

ע��: ����ֻ��һ�������ϵ�ģ�͡�������ͨ���Ա�ʵ�ʱȽ�ÿ�����ܵ�����Ը���Ч�ķ�ʽִ�У� ����Щ���û��������ġ�

����������������IJ�ѯ��ʵ�֣�

SELECT *
    FROM weather, cities
    WHERE city = name;

     city      | temp_lo | temp_hi | prcp |    date    |     name      | location
---------------+---------+---------+------+------------+---------------+-----------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
 San Francisco |      43 |      57 |    0 | 1994-11-29 | San Francisco | (-194,53)
(2 rows)

�۲��������������棺

��ϰ��. ����ʡ�� WHERE �Ӿ��������ʲô��

��Ϊ��Щ�ֶε����ֶ���һ�������Է������Զ��ҳ����������ĸ��� ���������Ӳ�ѯ��ʹ���ֶ�ȫ���Ǻܺõķ��

SELECT	weather.city, weather.temp_lo, weather.temp_hi,
	weather.prcp, weather.date, cities.location
    FROM weather, cities
    WHERE cities.name = weather.city;

��ĿǰΪֹ���������͵����Ӳ�ѯҲ������������������ʽд������

SELECT *
    FROM weather INNER JOIN cities ON (weather.city = cities.name);

����﷨�����������Ǹ���ô���ã�����������д������Ϊ������������˽��������⡣

�������ǽ���������ܰ�Hayward��¼�һ����� �������ò�ѯ�ɵ�����ɨ�� weather �� ���Ҷ�ÿһ�ж��ҳ�ƥ��� cities ��������С� �������û���ҵ�ƥ����У���ô������ҪһЩ"��ֵ"����cities����ֶΡ� �������͵IJ�ѯ���������� �������ڴ�֮ǰ���������Ӷ����ڲ����ӡ������������������������

SELECT *
    FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);

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

�����ѯ��һ�����ֱ��������� ��Ϊ�����Ӳ���������ע��LEFT OUTER JOIN�����ֱߵı��е��������������Ҫ����һ�Σ� �������ֱߵ��н�ֻ�����Щ�����ֱ����ж�Ӧƥ����С� �����������ֱ߱����û�ж�Ӧƥ������ֱ߱���У���ô�����ֱ��е��ֶν����գ�NULL����

��ϰ��. ���������Ӻ�ȫ���ӡ������ҳ��������ܸ�ʲô��

����Ҳ���԰�һ������Լ������������������������ ���磬�����������ҳ���Щ������������¼���¶ȷ�Χ֮���������¼�� �������Ǿ���Ҫ�� weather ����ÿ�е� temp_lo �� temp_hi �ֶ��� weather ���������е� temp_lo �� temp_hi �ֶν��бȽϡ����ǿ���������IJ�ѯʵ�����Ŀ�꣺

SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,
    W2.city, W2.temp_lo AS low, W2.temp_hi AS high
    FROM weather W1, weather W2
    WHERE W1.temp_lo < W2.temp_lo
    AND W1.temp_hi > W2.temp_hi;

     city      | low | high |     city      | low | high
---------------+-----+------+---------------+-----+------
 San Francisco |  43 |   57 | San Francisco |  46 |   50
 Hayward       |  37 |   54 | San Francisco |  46 |   50
(2 rows)

���������ǰ�weather�����±��Ϊ W1 �� W2 ���������ӵ����ֱߺ����ֱߡ� �㻹�����������ı�����������ѯ���ԼһЩ�ü������磺

SELECT *
    FROM weather w, cities c
    WHERE w.city = c.name;

���Ժ�ᾭ��������������д�ģ�