2012-02-02 48 views
2

查詢在PostGIS的,我有一對夫婦有相同結構的表,但分別站在不同層(1,2,3 ......)的Mapserver的,多個表上的PostGIS

table1 
     gid | name  | address  | post code | layer | geom 
-----------+----------+---------------+------------+-------+------------ 
     1 'name11'  'address11' 102356  1  geom11 
     2 'name12'  'address12' 102356  1  geom12 
     - 'name1-'  'address1-' 102356  1  geom1- 

table2 
     gid | name  | address  | post code | layer | geom 
-----------+----------+---------------+------------+-------+------------ 
     1 'name21'  'address21' 102356  2  geom21 
     2 'name22'  'address22' 102356  2  geom22 
     - 'name2-'  'address2-' 102356  2  geom2- 


table3 
     gid | name  | address  | post code | layer | geom 
-----------+----------+---------------+------------+-------+------------ 
     1 'name31'  'address31' 102356  3  geom31 
     2 'name32'  'address32' 102356  3  geom32 
     - 'name3-'  'address3-' 102356  3  geom3- 

我想從表1,2,3查詢結果......如果關鍵字匹配,說像「名稱」名稱如下:

輸入「姓名」,結果就會像

results 
     gid | name  | address  | post code | layer | geom 
-----------+----------+---------------+------------+-------+------------ 
     1 'name11'  'address11' 102356  1  geom11 
     2 'name12'  'address12' 102356  1  geom12 
     3 'name21'  'address21' 102356  2  geom21 
     4 'name22'  'address22' 102356  2  geom22 
     5 'name31'  'address31' 102356  3  geom31 
     6 'name32'  'address32' 102356  3  geom32 
     -  '-'   '-'   -----  -  -- 

我只需要在GID從1遞增順序重新編號,獲取他們原來的gid值似乎沒有必要:)

我可以用一個單一的查詢語句實現這個結果嗎?如何? 任何好的想法將被appricated?

感謝

回答

3

嘗試使用UNION ALLrow_number功能類似這樣的查詢:

SELECT 
row_number() over (ORDER BY a.gid, a.layer) AS qid, 
a.name, a.address, a."post code", a.layer, a.geom 
FROM 
(
SELECT * FROM table1 
UNION ALL 
SELECT * FROM table2 
UNION ALL 
SELECT * FROM table3 
)a 
WHERE a.name like 'name%' 
+0

taudorf,非常感謝 – tiplip 2012-02-03 01:15:49