我遇到了如何在sql的select语句中连接以下表的语法问题。 关系如下:表 a 具有 b 和 d 的内部联接。表 b 还具有 c 的内部联接。表 e 使用 b 和 d 中的字段左连接 a,b,c,d 。< / p>
这是代码。重要的部分是在底部。其余的只是表格和计算字段中的字段。它几乎可以工作,因为它可以检索数据。然而,当删除Futures_Orders行时,基础是删除Broker_Commissions行时,有一些不太正确。请记住,Futures_Orders上没有任何其他表的删除级联设置。
SELECT "Futures_Orders"."Date"
,"Futures_Orders"."Symbol"
,"Futures_Orders"."Contract_Mth"
,"Futures_Orders"."Send_To_Broker"
,"Futures_Orders"."Order-Id"
,"Futures_Orders"."Broker"
,"Futures_Orders"."Order_Action"
,"Futures_Orders"."No_Contracts"
,"Futures_Orders"."Order_Price"
,"Futures_Orders"."Trade_Type"
,CASE "Futures_Orders"."Order_Action"
WHEN 'b'
THEN 's'
ELSE 'b'
END "If_Done_Action"
,"Futures_Orders"."If_Done_Price"
,"Futures_Orders"."No_Contracts" * (
CASE "Contract_Details"."USIntRates"
WHEN 'n'
THEN ABS("Futures_Orders"."Order_Price" - "Futures_Orders"."If_Done_Price") / "Contract_Details"."MinTick"
ELSE ABS(ABS(TRUNCATE (
"Futures_Orders"."Order_Price"
,0
) - TRUNCATE (
"Futures_Orders"."If_Done_Price"
,0
)) * "Contract_Details"."MinTick" - CASE
WHEN "Contract_Details"."Symbol" IN (
'FV'
,'TU'
)
THEN 400
ELSE 200
END * ABS("Futures_Orders"."Order_Price" - TRUNCATE (
"Futures_Orders"."Order_Price"
,0
) - "Futures_Orders"."If_Done_Price" + TRUNCATE (
"Futures_Orders"."If_Done_Price"
,0
)))
END * "Contract_Details"."MinTickVal" / "FX_Rates"."Rate" + CASE
WHEN "Broker_Commissions"."Commission" IS NULL
OR "Broker_Commissions"."Commission_AUD" + "Broker_Commissions"."Commission" = 0
THEN "Broker"."Commission"
ELSE "Broker_Commissions"."Commission_AUD" + "Broker_Commissions"."Commission" / "FX_Rates"."Rate"
END * 2
) "Risk"
,"Futures_Orders"."Trade_Reason"
,"Contract_Details"."MinTick"
,"Contract_Details"."MinTickVal"
,"Contract_Details"."Symbol"
,"Contract_Details"."Currency"
,"FX_Rates"."Code"
,"Broker"."Broker"
,"Broker_Commissions"."Symbol"
,"Broker_Commissions"."Broker"
,"Broker_Commissions"."ID"
FROM "Futures_Orders"
,"Contract_Details"
,"Broker"
,"FX_Rates"
LEFT JOIN "Broker_Commissions" ON "Broker_Commissions"."Broker" = "Broker"."Broker"
AND "Broker_Commissions"."Symbol" = "Contract_Details"."Symbol"
WHERE "Contract_Details"."Symbol" = "Futures_Orders"."Symbol"
AND "Broker"."Broker" = "Futures_Orders"."Broker"
AND "FX_Rates"."Code" = "Contract_Details"."Currency"
答案 0 :(得分:1)
我认为你需要类似的东西。
<field>
对于每种关系都不同,但你没有提供
SELECT
FROM a
INNER JOIN b
ON a.<field> = b.<field>
INNER JOIN d
ON a.<field> = d.<field>
INNER JOIN c
ON b.<field> = c.<field>
LEFT JOIN e
ON e.<field> = b.<field>
AND e.<field> = d.<field>
答案 1 :(得分:0)
假设您要连接的列在两个表中都被命名为相同(并且没有重复导致歧义),我们可以使用NATURAL来简化语法。
d NATURAL LEFT JOIN (a NATURAL JOIN (b NATURAL JOIN c) NATURAL JOIN d)
您可以使用NATURAL ... JOIN X
语法
JOIN X ON (COLA = COLB)
您的实际示例不同,可以这样写:
FROM "Futures_Orders" NATURAL JOIN "Contract_Details"
NATURAL JOIN "Broker"
INNER JOIN "FX_Rates" ON ("FX_Rates"."Code" = "Contract_Details"."Currency")
NATURAL LEFT JOIN "Broker_Commissions"
如果使用NATURAL连接,则必须在查询中引用同名的列,而不使用其表名。
你提到Base(OpenOffice或LibreOffice),它有自己的预处理器和语法差异,可以修改查询,有时不正确。