2016-10-18 35 views
0

如何根據不同菜單中的名稱從兩個表中進行選擇,但在PSQL中數據幾乎相同? 我試過使用select和從psql命令進行替換。PSQL選擇和替換字符

SELECT * FROM americanmenu JOIN europeanmenu ON replace(usmenu.type, \'US\', \'EU\') = eumenu.type WHERE usmenu.type = eumenu.type  

表美國

ID | type | year 
---------------------- 
01 | wine 1 us | 2001 
02 | wine 2 us | 2002 

表歐盟

ID | TYPE | year 
-------------------- 
01 | wine 1 eu | 2001 
02 | wine 2 eu | 2002 

還有額外的價格和口味評級列,我沒有包括,因爲這是問題的要點。我想按類型從us表中選擇,並將最後2個字符/字符串替換爲「eu」,並且即使存在大量相同的數據也能夠比較兩個表。謝謝!

回答

0

查詢:

t=# SELECT * 
FROM americanmenu a 
JOIN europeanmenu e ON replace(a.type, 'us','eu') = e.type; 
id | type  | year | id | type  | year 
-----+-------------+------+-----+-------------+------ 
01 | wine 1 us | 2001 | 01 | wine 1 eu | 2001 
02 | wine 2 us | 2002 | 02 | wine 2 eu | 2002 
(2 rows) 

Time: 0.297 ms 

製備:

t=# create table americanmenu (id text, type text, year int); 
CREATE TABLE 
Time: 4.515 ms 
t=# create table europeanmenu (id text, type text, year int); 
CREATE TABLE 
Time: 15.218 ms 
t=# copy americanmenu from stdin delimiter '|'; 
Enter data to be copied followed by a newline. 
End with a backslash and a period on a line by itself. 
>> 01 | wine 1 us | 2001 
02 | wine 2 us | 2002>> 
>> \. 
COPY 2 
Time: 7144.563 ms 
t=# copy europeanmenu from stdin delimiter '|'; 
Enter data to be copied followed by a newline. 
End with a backslash and a period on a line by itself. 
>> 01 | wine 1 eu | 2001 
02 | wine 2 eu | 2002>> 
>> \. 
COPY 2 
Time: 9729.000 ms