-1
我理解此相關子查詢的問題:SQL - 相關子查詢
SELECT Country, CustomerID,
(SELECT COUNT(*)
FROM Customers AS CustomersInner
WHERE CustomersInner.CustomerID < CustomersOuter.CustomerID
AND CustomersInner.Country=CustomersOuter.Country) + 1
AS customer_seq_num
FROM Customers AS CustomersOuter ORDER BY Country;
這個例子來自http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
如何做到這一點的查詢工作是什麼呢?爲什麼要比較CustomersInner.CustomerID < CustomersOuter.CustomerID?如何解釋+1?更深層的解釋會很棒。
在大多數數據庫中,您可以使用'rank()'來達到此目的。我建議你研究窗口函數。 –