How to best check for NULL? #206
-
|
I have a basic database schema with 3 tables: Book, Author and BookAuthor. I'm trying to get a count of Books that do not have a related Author. The SQL query I'm trying to implement to solve this problem is this: SELECT COUNT(*) from book
LEFT JOIN book_author on book_author.bookId = book.id
WHERE book_author.id is null;When attempting to build this query I'm not finding any examples of checking for Book
.leftJoin(BookAuthor.all) { $0.id.eq($1.bookId) }
// WHERE clauses I've tried
.where { #sql("\($1.id) IS NULL") } // Compiles, but value is wrong
.where { $1.id.eq(nil) } // throws "Generic parameter 'some QueryExpression<Self.QueryValue>' could not be inferred"
.where { $1.id.eq(UUID(uuidString: "")) } // Compiles, but value is wrong
// SELECT clauses I've tried
.select { $1.id.count() }
.select { $1.id.count(filter: #sql("\($1.id) IS NULL")) }, // Compiles, but value is wrongAppreciate any help with this! |
Beta Was this translation helpful? Give feedback.
Answered by
stephencelis
Oct 10, 2025
Replies: 1 comment 2 replies
-
|
There's an Book
.select { $0.count() }
.leftJoin(BookAuthor.all) { $0.id.eq($1.bookId) }
.where { $1.id.is(nil) } |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
carbonsam
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's an
isoperator fornilchecking. Does this work for you?