我在Windows 7 x64上運行Postgres 9.1.3 32位。 (已使用32位因爲沒有Windows的PostGIS版本與64個Postgres的兼容。)(編輯:作爲PostGIS的2.0,它是與Windows Postgres的64位兼容)Postgres LEFT JOIN正在創建比左表更多的行
我有一個查詢, left將表(consistent.master
)與臨時表結合,然後將結果數據插入第三個表(consistent.masternew
)。
由於這是一個left join
,因此生成的表應該具有與查詢中左表相同數量的行。但是,如果我運行這個:
SELECT count(*)
FROM consistent.master
我得到2085343
。但如果我運行這個:
SELECT count(*)
FROM consistent.masternew
我得到2085703
。
masternew
怎麼能有比master
更多的行?不應該masternew
具有與master
相同的行數,查詢中的左表是?
以下是查詢。 master
和masternew
表應該具有相同的結構。
--temporary table created here
--I am trying to locate where multiple tickets were written on
--a single traffic stop
WITH stops AS (
SELECT citation_id,
rank() OVER (ORDER BY offense_timestamp,
defendant_dl,
offense_street_number,
offense_street_name) AS stop
FROM consistent.master
WHERE citing_jurisdiction=1
)
--Here's the insert statement. Below you'll see it's
--pulling data from a select query
INSERT INTO consistent.masternew (arrest_id,
citation_id,
defendant_dl,
defendant_dl_state,
defendant_zip,
defendant_race,
defendant_sex,
defendant_dob,
vehicle_licenseplate,
vehicle_licenseplate_state,
vehicle_registration_expiration_date,
vehicle_year,
vehicle_make,
vehicle_model,
vehicle_color,
offense_timestamp,
offense_street_number,
offense_street_name,
offense_crossstreet_number,
offense_crossstreet_name,
offense_county,
officer_id,
offense_code,
speed_alleged,
speed_limit,
work_zone,
school_zone,
offense_location,
source,
citing_jurisdiction,
the_geom)
--Here's the select query that the insert statement is using.
SELECT stops.stop,
master.citation_id,
defendant_dl,
defendant_dl_state,
defendant_zip,
defendant_race,
defendant_sex,
defendant_dob,
vehicle_licenseplate,
vehicle_licenseplate_state,
vehicle_registration_expiration_date,
vehicle_year,
vehicle_make,
vehicle_model,
vehicle_color,
offense_timestamp,
offense_street_number,
offense_street_name,
offense_crossstreet_number,
offense_crossstreet_name,
offense_county,
officer_id,
offense_code,
speed_alleged,
speed_limit,
work_zone,
school_zone,
offense_location,
source,
citing_jurisdiction,
the_geom
FROM consistent.master LEFT JOIN stops
ON stops.citation_id = master.citation_id
在它的事項的情況下,我已經運行VACUUM FULL ANALYZE
和重建索引兩個表。 (不清楚確切的命令;通過pgAdmin III完成。)
謝謝。就是這樣。 'LEFT JOIN'意味着連接中的*最小*行數將是左表的數量。 – 2012-03-19 02:22:27