2017-02-23 138 views
1

在下面的示例中,如果我查詢我的視圖並給出具體的ident,那麼它會正確返回並說明訂單已完成。SQL查詢根據字段返回不同的結果

如果我然後要求所有尚未完成的訂單,它會返回ident,它顯然剛剛看到WAS完成。這怎麼可能/

# select complete from orders_plus where ident = 8410; 
complete 
---------- 
t 
(1 row) 

# select ident,complete from orders_plus where complete = false; 
ident | complete 
-------+---------- 
    8402 | f 
    8410 | f 
    8414 | f 
(3 rows) 

這裏的視圖的定義:

# \d+ orders_plus 
         View "public.orders_plus" 
    Column |  Type  | Modifiers | Storage | Description 
--------------+-------------------+-----------+----------+------------- 
ident  | integer   |   | plain | 
station_id | integer   |   | plain | 
created  | date    |   | plain | 
due   | date    |   | plain | 
month  | date    |   | plain | 
comments  | character varying |   | extended | 
name   | character varying |   | extended | 
ud   | real    |   | plain | 
dp   | real    |   | plain | 
swrv   | real    |   | plain | 
sh   | real    |   | plain | 
jmsw   | real    |   | plain | 
sw   | real    |   | plain | 
prrv   | real    |   | plain | 
mhsw   | real    |   | plain | 
bmsw   | real    |   | plain | 
mp   | real    |   | plain | 
pr   | real    |   | plain | 
st   | real    |   | plain | 
rsw   | real    |   | plain | 
rsh   | real    |   | plain | 
rpr   | real    |   | plain | 
rmp   | real    |   | plain | 
total_points | real    |   | plain | 
complete  | boolean   |   | plain | 
View definition: 
SELECT o.ident, o.station_id, o.created, o.due, o.month, o.comments, o.name, 
    o.ud, o.dp, o.swrv, o.sh, o.jmsw, o.sw, o.prrv, o.mhsw, o.bmsw, o.mp, o.pr, 
    o.st, o.rsw, o.rsh, o.rpr, o.rmp, o.total_points, 
    COALESCE(sum(w.ud) >= o.ud AND sum(w.dp) >= o.dp AND sum(w.swrv) >= o.swrv AND sum(w.sh) >= o.sh AND sum(w.jmsw) >= o.jmsw AND sum(w.sw) >= o.sw AND sum(w.prrv) >= o.prrv AND sum(w.mhsw) >= o.mhsw AND sum(w.bmsw) >= o.bmsw AND sum(w.mp) >= o.mp AND sum(w.pr) >= o.pr AND sum(w.st) >= o.st AND sum(w.rsw) >= o.rsw AND sum(w.rsh) >= o.rsh AND sum(w.rpr) >= o.rpr AND sum(w.rmp) >= o.rmp, false) AS complete 
    FROM orders o 
    LEFT JOIN production_work w ON o.ident = w.order_id 
    GROUP BY o.ident; 
+1

我的第一印象 - 索引損壞。如果表在列上有索引,請嘗試'reindex orders_plus'。 – klin

+0

'\ d + orders_plus'上的索引是什麼?.. –

+0

這是一個視圖,它似乎沒有索引。我更新了問題以顯示\ d +輸出 – Gargoyle

回答

-1

的ident是不是主鍵。你有兩個相同的記錄(至少)一個是t,一個是f。

+0

再次,這是一個VIEW,因此沒有主鍵。訂單表具有標識作爲主鍵,並且您可以從第一個選擇我看到只有一個具有該標識的條目。 – Gargoyle