doug-martin / goqu

SQL builder and query library for golang
http://doug-martin.github.io/goqu/
MIT License
2.36k stars 205 forks source link

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

Open makholm opened 3 years ago

makholm commented 3 years ago

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()...

crashiura commented 3 years ago

@makholm hi, try this

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