Skip to content

FromClause

MSugiura edited this page Jan 21, 2024 · 5 revisions

You can set a table name or alias name in the From clause.Table join expressions are also included in the From clause.

From

After setting the table name as the first argument in the From clause, use method chaining to call the As method and set the alias name.Return types are FromaClause and SelectableTable.

FromaClause is used when joining tables. If you are not using table joins, you do not need to name the variable. There is no naming rule for variables, but the demo site uses "f" or "from".

SelectableTable is often used for column selection, join expressions, search conditions, etc., so it is recommended that the variable name be the same as the table alias name.

using System;
using Carbunql;
using Carbunql.Building;
                    
public class Program
{
    public static void Main()
    {
        var sq = new SelectQuery();
        var (f, a) = sq.From("table_a").As("a");
        sq.SelectAll();
        Console.WriteLine(sq.ToText());                                
    }
}

SubQuery

Specifying SelectQuery in the From clause will refer to a subquery.You can wrap an existing SelectQuery and perform processing such as filtering.

using System;
using Carbunql;
using Carbunql.Building;
                    
public class Program
{
    public static void Main()
    {
        var x = new SelectQuery("select * from table_x");
		
        var sq = new SelectQuery();
        var (f, a) = sq.From(x).As("a");
        sq.SelectAll();
        Console.WriteLine(sq.ToText());                                
    }
}
SELECT
    *
FROM
    (
        SELECT
            *
        FROM
            table_x
    ) AS a

ValuesQuery

You can also do a ValuesQuery. Since there are no column names in ValuesQuery, we recommend using the ToSelectableTable method to set column aliases.

using System;
using Carbunql;
using Carbunql.Building;
using Carbunql.Analysis;
                    
public class Program
{
    public static void Main()
    {
        var x = ValuesQueryParser.Parse("values(1,2,3),(4,5,6)");
        var sq = new SelectQuery();
        var (f, a) = sq.From(x.ToSelectableTable(new[] { "v1", "v2", "v3" })).As("a");
        sq.SelectAll();
        Console.WriteLine(sq.ToText());                                
    }
}
SELECT
    *
FROM
    (
        VALUES
            (1, 2, 3),
            (4, 5, 6)
    ) AS a (
        v1, v2, v3
    )

CTE

CTE can also be specified.

using System;
using Carbunql;
using Carbunql.Building;
                    
public class Program
{
    public static void Main()
    {
        var sq = new SelectQuery();
        var x = sq.With("select * from table_x").As("x");
        var (f, a) = sq.From(x).As("a");
        sq.SelectAll();
        Console.WriteLine(sq.ToText());                                
    }
}
WITH
    x AS (
        SELECT
            *
        FROM
            table_x
    )
SELECT
    *
FROM
    x AS a
Clone this wiki locally