SQLKata 多个来自子选择

SQLKata Multiple From Sub Selects

我正在尝试编写以下语句是我想要的输出:

SELECT TABLE1.COLUMN_1
  FROM (SELECT '0' AS COLUMN_1 FROM DUAL) TABLE1,
       (SELECT '0' AS COLUMN_1 FROM DUAL) TABLE2
 WHERE TABLE1.COLUMN_1 = TABLE2.COLUMN_1;

这是代码 SQLKata:

<PackageReference Include="SqlKata" Version="3.2.3" />
<PackageReference Include="SqlKata.Execution" Version="3.2.3" />

using SqlKata;
using SqlKata.Execution;

var q1= new Query("DUAL").Select("'0' AS COLUMN_1");
var q2= new Query("DUAL").Select("'0' AS COLUMN_1");
var q3= new Query().From(q1, "table1")
                   .From(q2, "table2")
                   .Where("table1.COLUMN_1","table2.COLUMN_1")
                   .Select("table1.COLUMN_1")
                   .FirstOrDefault<string>();

这导致:

SELECT TABLE1.COLUMN_1
  FROM (SELECT '0' AS COLUMN_1 FROM DUAL) TABLE2
 WHERE TABLE1.COLUMN_1 = TABLE2.COLUMN_1;

好像第二个From覆盖了第一个From。

另一个例子:

var phones = new Query("Phones");
var laptops = new Query("Laptops");

var query = new Query().From(phones, "P").From(laptops, "L") 

结果,通知 Phones 不在查询中:

SELECT
  *
FROM
  (
    SELECT
      *
    FROM
      [Laptops]
  ) AS [L]

这基本上是一个内连接查询,因此您可以像这样重写它:

SELECT TABLE1.COLUMN_1
  FROM (SELECT '0' AS COLUMN_1 FROM DUAL) TABLE1 JOIN
       (SELECT '0' AS COLUMN_1 FROM DUAL) TABLE2
 ON TABLE1.COLUMN_1 = TABLE2.COLUMN_1;

这是 SqlKata 中的等效语法

var q1 = new Query("DUAL").Select("'0' AS COLUMN_1");
var q2 = new Query("DUAL").Select("'0' AS COLUMN_1");

var query = new Query()
    .From(q1, "table1")
    .Join(
        q2.As("table2"), // note how the As() method is used to alias this sub query 
        j => j.On("table1.COLUMN_1","table2.COLUMN_1")
    )
    .Select("table1.COLUMN_1");