Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IFNULL in sqlalchemy core

I'm trying to use sqlalchemy core select rows from a mysql table using IFNULL.

Given a table like so:

id    int1    string1   other
1      7        NULL    other stuff
2      NULL     bar     more stuff 

The sql would be something like:

SELECT IFNULL(int1, 0) AS int1, IFNULL(string1, '') AS string1 FROM table

Is this possible using the core? What would be great would be something like

s = select(ifnull(table.c.int1, 0), ifnull(table.c.string1, ''))
like image 892
Luke Avatar asked Jul 31 '15 21:07

Luke


1 Answers

PostgreSQL doesn't support if null

instead of ifnull() you can use func.coalesce()

Syntax -

  func.coalesce(Table.field, default_value)

Example -

 func.coalesce(Order.price, 0)

 func.coalesce(Student.name, ' ')
like image 185
Mohammad Aarif Avatar answered Nov 06 '22 01:11

Mohammad Aarif