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, ''))
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, ' ')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With