goqu icon indicating copy to clipboard operation
goqu copied to clipboard

How to SELECT "col" IS NOT NULL AS "alias" ?

Open makholm opened this issue 4 years ago • 1 comments

Is there a way to alias a boolean expression in the select column list?

I think what I want is basically:

db.From("foo").Select(goqu.I("foo.bar").IsNotNull().As("have_bar")

But type exp.BooleanExpression does not implement As(). I can work around it using a functional expression. This generates the correct SQL, though with extra parenthesis:

Select(goqu.Func("", goqu.I("foo.bar").IsNotNull()).As("have_bar"))

...but I'm hoping there's prettier way that doesn't involve abusing Func()...

makholm avatar Jan 19 '21 11:01 makholm

@makholm hi, try this

goqu.From("foo").Select(
		goqu.L(`foo.bar IS NOT NULL`).As("some_field"),
	)

crashiura avatar Feb 20 '21 15:02 crashiura