聯合查詢在前面的例子中已經出現過,只不過沒有細說。
先來個例子吧
DbSession.Default.From<Customers>()
.InnerJoin<Orders>(Customers._.CustomerID == Orders._.CustomerID)
.ToDataTable();
生成的sql
Text:
SELECT * FROM [Customers] INNER JOIN [Orders] ON ([Customers].[CustomerID] = [Orders].[CustomerID])
是非常簡單操作就完成了。
方法 聯合查詢(sql) InnerJoin inner join LeftJoin left join RightJoin right join CrossJoin cross join FullJoin full join Union union UnionAll union all
還是上例子清爽一些
DbSession.Default.From<Customers>()
.LeftJoin<Orders>(Customers._.CustomerID == Orders._.CustomerID)
.Where(Customers._.Country == "USA")
.ToDataTable();
生成的sql
Text:
SELECT * FROM [Customers]
LEFT OUTER JOIN [Orders]
ON ([Customers].[CustomerID] = [Orders].[CustomerID])
WHERE [Customers].[Country] = @e3c66f9aa65c498abfd76908621b567a
Parameters:
@e3c66f9aa65c498abfd76908621b567a[String] = USA
InnerJoin、LeftJoin、RightJoin、CrossJoin、FullJoin這個操作都是類似的。
三個表及以上關聯例如
DbSession.Default.From<Customers>()
.InnerJoin<Orders>(Customers._.CustomerID == Orders._.CustomerID)
.InnerJoin<Order_Details>(Order_Details._.OrderID == Orders._.OrderID)
.Where(Customers._.Country == "USA")
.ToDataTable();