這是我寫的,但我沒有得到正確的答案:
select top 1 customer.first_name
from customer, claims
where customer.id = claims.id
order by claims.amount_of_claim desc
GO

uj5u.com熱心網友回復:
您正在加入claim.id和customer.id。除非偶然,否則它們永遠不會匹配。除非意外,索賠檔案上的號碼永遠不會與您的保險卡或保單上的號碼相符。
撰寫查詢時,僅根據查詢運算式匹配表行,而不是表之間的任何約束。這意味著以下查詢將嘗試匹配不相關的事物:
select top 1 customer.first_name
from customer inner join claims
on customer.id = claims.id
order by claims.amount_of_claim desc
該圖顯示索賠與保單相關,保單又與客戶相關:
Claims(customer_policy_id) -> Customer_Policy(customer_id) -> Customer
您必須在查詢中加入這 3 個表
select top 1 cust.first_name
from customer cust
inner join customer_policy pol on cust.id=pol.customer_id
inner join claims cl on pol.id=cl.customer_policy_id
order by cl.amount_of_claim desc
在關系資料庫中,關系由表表示,而不是外鍵約束。外鍵約束用于確保存盤在表中的 FK 值有效。
最重要的是,ER 圖不是資料庫圖。ER 圖中顯示的物體和關系不直接映射到表。Customer例如,ER圖中的eg和之間的多對多關系Address必須轉換為橋接表,CustomerAddresses(CustomerId,AddressId). 畢竟,表是關系理論中的關系,CustomerAddresses定義了 Customer 和 Address 之間的關系。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/416745.html
標籤:
上一篇:添加更多ID時SQL查詢速度變慢
下一篇:具有2個值的SQLOery引數
