2015-11-27 54 views
0

我在postgres中有一個查詢問題,表cgporders_items有一個名爲return的字段,我不能用這個查詢得到該字段的實際結果,它返回我al ceros 。在postgreSQL中選擇一個名爲「return」的字段

SELECT "Cgporder".id AS "Cgporder__id" 
    ,"Sale".preorder_number AS "Sale__preorder_number" 
    ,"Contact".id AS "Contact__id" 
    ,"Contact".NAME AS "Contact__name" 
    ,"Ptype".NAME AS "Ptype__name" 
    ,(
     SELECT code 
     FROM products 
     WHERE id = "CgporderItem".parent_id 
     ) AS "Product__parent_code" 
    ,"Product".id AS "Product__id" 
    ,"Product".code AS "Product__code" 
    ,"Product".NAME AS "Product__name" 
    ,"CgporderItem".quantity AS "CgporderItem__quantity" 
    ,"CgporderItem".return AS "CgporderItem__return" 
    ,"CgporderItem".cep_id AS "CgporderItem__cep" 
FROM cgporders AS "Cgporder" 
INNER JOIN contacts AS "Contact" ON ("Contact".id = "Cgporder".contact_id) 
INNER JOIN cgporders_items AS "CgporderItem" ON ("Cgporder".id = "CgporderItem".cgporder_id) 
INNER JOIN products AS "Product" ON ("Product".id = "CgporderItem".product_id) 
INNER JOIN ptypes AS "Ptype" ON ("Ptype".id = "Product".ptype_id) 
LEFT JOIN cgporders_sales AS "CgporderSale" ON ("Cgporder".id = "CgporderSale".cgporder_id) 
LEFT JOIN sales AS "Sale" ON ("Sale".id = "CgporderSale".sale_id) 
WHERE "CgporderItem".parent_id != 0 
    AND "Cgporder"."issue_date" >= '2015-11-27' 
    AND "Cgporder"."issue_date" <= '2015-11-27' 
    AND "Cgporder"."status" = 'confirmed' 
ORDER BY "Ptype".NAME 
    ,"Product"."code"; 

實際上有大量的行符合選擇條件,但它的「CgporderItem」迴歸CERO .return AS「CgporderItem__return」

如果我做出這樣選擇「迴歸」一個簡單的查詢從cgporders_items它的工作原理。但在這個查詢中它不起作用。

你能幫助我嗎?

+0

您是否試過''CgporderItem「。」return「AS」CgporderItem__return「'?返回是一個保留字,它必須被引用。 – fvu

+0

是的,我已經嘗試過,但它也不起作用 – juanpscotto

回答

1

這太長了評論。

「return」是SQL中的保留字,但不是Postgres中的保留字。請參閱列表here。下面的代碼工作發現的Postgres(SQL小提琴是here):

create table dum (return int); 

select dum.return from dum; 

您的問題是別的東西。如果我不得不猜測,where子句太嚴格了(日期條件有點懷疑)。

相關問題