sqlkata / querybuilder

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
https://sqlkata.com
MIT License
3.08k stars 498 forks source link

How to compare results of two subqueries in where clause SQLKata #653

Closed sterlyukin closed 1 year ago

sterlyukin commented 1 year ago

I try to compare results of two subqueries in where clause in SQLKata.

In SQL it should be like this:

WHERE (SELECT count(id) FROM main.someTable) = (SELECT count(id) FROM main.anotherTable)

In SQLKata I can compare result of subquery with scalar value:

var mainSubquery = new Query("main.someTable")
            .SelectRaw("count(id)");

var anotherSubquery = new Query("main.anotherTable")
            .SelectRaw("count(id)");

query
    .WhereSub(mainSubquery, "=", 0)

But I can't compare results of two subqueries this way:

query
    .WhereSub(mainSubquery, "=", anotherSubquery)

How can I fix it? Maybe I should execute both of the subqueries and only then compare their results? Thanks for response in advance.

sterlyukin commented 1 year ago

Solved here - https://stackoverflow.com/questions/74939077/how-to-compare-result-of-two-subqueries-in-where-clause-sqlkata/74948798#74948798