Skip to content

Latest commit

 

History

History
65 lines (45 loc) · 1.33 KB

from.md

File metadata and controls

65 lines (45 loc) · 1.33 KB

From

From a Table or View

The Query constructor takes an optional parameter to set the from clause

//:playground
new Query("Posts");

Or you can use the From method to set it

//:playground
new Query().From("Posts");
SELECT * FROM [Posts]

Alias

To alias the table you should use the as syntax

//:playground
new Query("Posts as p")
SELECT * FROM [Posts] AS [p]

From a Sub Query

You can select from a sub query by passing a Query instance to the From method or you can use the Lambda function overload.

//:playground
var fewMonthsAgo = DateTime.UtcNow.AddMonths(-6);
var oldPostsQuery = new Query("Posts").Where("Date", "<", fewMonthsAgo).As("old");

var query = new Query().From(oldPostsQuery).OrderByDesc("Date");
SELECT * FROM (SELECT * FROM [Posts] WHERE [Date] < '2017-06-01 6:31:26') AS [old] ORDER BY [Date] DESC

From a Raw expression

The FromRaw method let you write raw expressions.

for example in SqlServer you can use the TABLESAMPLE to get a 10% sample of the total rows in the comments table.

    var query = new Query().FromRaw("Comments TABLESAMPLE SYSTEM (10 PERCENT)")
SELECT * FROM Comments TABLESAMPLE SYSTEM (10 PERCENT)

Note: Remember you can use the [] to wrap identifiers word in your string