Subquery in Where clause SQLKata

637 Views Asked by At

I try to pass subquery to where clause in SQLKata and compare it with 0 value.

In SQL it should be like this:

WHERE (SELECT count(id) FROM main.someTable) > 0

I try to reproduce it in SQLKata by this code:

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

return query
    .Where("0", "<", countSubQuery);

But I get error message:

column \"'0'\" does not exist

How can I fix this? Maybe I should pass zero value by another way?

2

There are 2 best solutions below

2
On
var countSubQuery = 
    new Query("main.someTable")
        .SelectRaw("count(id)");

return query
    .Where(Sql("0"), "<", countSubQuery);
0
On

I could solve my problem:

query = query
         .WhereSub(countSubQuery, ">", 0);