2013-05-04 32 views
0

在PostgreSQL 8.4.13我有兩個表格,分別代表紙牌遊戲(每個玩家有3個玩家)以及這些遊戲的得分結果。球員們通過id柱和遊戲鑑定gid在連接表上執行true

# \d pref_games 
            Table "public.pref_games" 
Column |   Type    |      Modifiers 
--------+-----------------------------+---------------------------------------------------------- 
gid | integer      | not null default nextval('pref_games_gid_seq'::regclass) 
rounds | integer      | not null 
stamp | timestamp without time zone | default now() 
Indexes: 
    "pref_games_pkey" PRIMARY KEY, btree (gid) 
Referenced by: 
    TABLE "pref_scores" CONSTRAINT "pref_scores_gid_fkey" FOREIGN KEY (gid) REFERENCES pref_games(gid) ON DELETE CASCADE 

# \d pref_scores 
     Table "public.pref_scores" 
Column |   Type   | Modifiers 
---------+-----------------------+----------- 
id  | character varying(32) | not null 
gid  | integer    | not null 
money | integer    | not null 
last_ip | inet     | 
quit | boolean    | 
Indexes: 
    "pref_scores_pkey" PRIMARY KEY, btree (id, gid) 
    "pref_scores_gid_index" btree (gid) 
Foreign-key constraints: 
    "pref_scores_gid_fkey" FOREIGN KEY (gid) REFERENCES pref_games(gid) ON DELETE CASCADE 
    "pref_scores_id_fkey" FOREIGN KEY (id) REFERENCES pref_users(id) ON DELETE CASCADE 

我試圖找出是否有id='OK261593357402'球員曾經與玩家id='MR3689735717800138910'發揮 - 這樣我就可以讓他們每個年級其他在我的網站。

我想我需要撥inner join上的perform true - 請問這是正確的嗎?

所以我想:

# select * from pref_games g inner join pref_scores s on (s.gid=g.gid) where s.id='OK261593357402'; 
    gid | rounds |   stamp   |  id  | gid | money | last_ip  | quit 
---------+--------+----------------------------+----------------+---------+-------+----------------+------ 
2124241 |  20 | 2013-05-01 12:26:54.052086 | OK261593357402 | 2124241 | 28 | 93.232.91.105 | f 
2125575 |  17 | 2013-05-01 16:53:21.090822 | OK261593357402 | 2125575 | 32 | 93.232.91.105 | f 
2125881 |  20 | 2013-05-01 17:47:26.15633 | OK261593357402 | 2125881 | -31 | 93.232.91.105 | f 
2126242 |  0 | 2013-05-01 18:41:06.769132 | OK261593357402 | 2126242 |  0 | 93.232.91.105 | f 
2126244 |  0 | 2013-05-01 18:41:12.495192 | OK261593357402 | 2126244 |  0 | 93.232.91.105 | t 
2126259 |  0 | 2013-05-01 18:42:39.974518 | OK261593357402 | 2126259 |  0 | 93.232.91.105 | t 
2126613 |  33 | 2013-05-01 19:27:11.88462 | OK261593357402 | 2126613 | -132 | 93.232.91.105 | f 
2126813 |  0 | 2013-05-01 19:57:05.23061 | OK261593357402 | 2126813 |  0 | 93.232.91.105 | t 
2127299 |  20 | 2013-05-01 20:36:42.021133 | OK261593357402 | 2127299 | 136 | 93.232.91.105 | f 
2127821 |  0 | 2013-05-01 21:33:32.168757 | OK261593357402 | 2127821 |  0 | 93.232.91.105 | f 
2127830 |  0 | 2013-05-01 21:34:47.694645 | OK261593357402 | 2127830 |  0 | 93.232.91.105 | t 
2128012 |  21 | 2013-05-01 22:04:03.850061 | OK261593357402 | 2128012 | 55 | 93.232.91.105 | f 
2129551 |  13 | 2013-05-02 10:08:37.348426 | OK261593357402 | 2129551 | -32 | 79.250.39.175 | f 
2129818 |  13 | 2013-05-02 11:21:50.998484 | OK261593357402 | 2129818 | 71 | 79.250.39.175 | f 
2130467 |  11 | 2013-05-02 13:55:00.034698 | OK261593357402 | 2130467 | -79 | 79.250.39.175 | f 
2130470 |  0 | 2013-05-02 13:55:41.298932 | OK261593357402 | 2130470 |  0 | 79.250.39.175 | f 
2130476 |  0 | 2013-05-02 13:56:22.359713 | OK261593357402 | 2130476 |  0 | 79.250.39.175 | f 
..... 

但我只看到了id='OK261593357402'以上,即我期待看到他所有的遊戲合作伙伴也一樣,但他們沒有交付。

我也試過:

# select * from pref_games g left outer join pref_scores s on (s.gid=g.gid) where s.id='OK261593357402'; 
    gid | rounds |   stamp   |  id  | gid | money | last_ip  | quit 
---------+--------+----------------------------+----------------+---------+-------+----------------+------ 
2124241 |  20 | 2013-05-01 12:26:54.052086 | OK261593357402 | 2124241 | 28 | 93.232.91.105 | f 
2125575 |  17 | 2013-05-01 16:53:21.090822 | OK261593357402 | 2125575 | 32 | 93.232.91.105 | f 
2125881 |  20 | 2013-05-01 17:47:26.15633 | OK261593357402 | 2125881 | -31 | 93.232.91.105 | f 
2126242 |  0 | 2013-05-01 18:41:06.769132 | OK261593357402 | 2126242 |  0 | 93.232.91.105 | f 
2126244 |  0 | 2013-05-01 18:41:12.495192 | OK261593357402 | 2126244 |  0 | 93.232.91.105 | t 
2126259 |  0 | 2013-05-01 18:42:39.974518 | OK261593357402 | 2126259 |  0 | 93.232.91.105 | t 
2126613 |  33 | 2013-05-01 19:27:11.88462 | OK261593357402 | 2126613 | -132 | 93.232.91.105 | f 
2126813 |  0 | 2013-05-01 19:57:05.23061 | OK261593357402 | 2126813 |  0 | 93.232.91.105 | t 
... 

不幸的是同樣的結果...

如果我也許只是做出一系列select s的exists in,而不是合併?

+0

什麼是'執行正確'?某種Oracle-ism或專有PostgreSQL擴展的功能? – 2013-05-04 12:13:17

+0

我的意思是:'從......執行......',然後'如果找到了...... ENDIF' – 2013-05-04 13:10:04

+0

哦,你正在談論PL/PgSQL的'PERFORM',這是SELECT的無結果返回替代方案',然後測試'FOUND'。好的,這是有道理的。你從來沒有說過任何關於PL/PgSQL或者你正在編寫一個存儲函數。 (這就是爲什麼在問題中顯示代碼是一個好主意的原因)。 – 2013-05-04 15:32:11

回答

1

我試圖找出是否有id='OK261593357402'一名球員 曾經與玩家id='MR3689735717800138910'

它通常是短/打出更快/更簡單/更優雅的使用IF EXISTS ...PLPGSQLPERFORM與尾隨IF FOUND ...

IF EXISTS (
    SELECT 1 
    FROM pref_scores p1 
    JOIN pref_scores p2 USING (gid) 
    WHERE p1.id = 'OK261593357402' 
    AND p2.id = 'MR3689735717800138910' 
    ) THEN 
    -- do stuff 
END IF; 

根本不需要表pref_games來回答你的問題。

+0

謝謝!而且你不需要'pref_games'表,它非常有見地:-) – 2013-05-08 07:43:52

1
select pg.*, ps.id, ps2.id 
from 
    pref_games pg 
    inner join 
    pref_scores ps on pg.gid = ps.gid 
    inner join 
    pref_scores ps2 on ps.gid = ps2.gid 
where 
    ps.id = 'OK261593357402' 
    and ps2.id != 'OK261593357402'