How to use modulo in sqlalchemy?
Question:
I have this raw SQL statement that I would like to use for both Core and ORM:
SELECT * FROM `buffet` WHERE `roomId` = '864495034004835' AND `recordMasa` > '1514600000' AND `recordMasa` < '1514900000' AND `recordMasa` mod 10 = 0 LIMIT 0,10000000000;
Please do let me know how can I add to my existing code below to include the modulo function:
select_statement = select([Buffet]).where(and_(
Buffet.recordMasa > arguments['startDate'],
Buffet.recordMasa < arguments['endDate'],
Buffet.roomId == arguments['ident']
))
rooms = conn.execute(select_statement).fetchall()
Answers:
what about the modulo operator from python?
select_statement = select([Buffet]).where(and_(
Buffet.recordMasa > arguments['startDate'],
Buffet.recordMasa < arguments['endDate'],
Buffet.roomId == arguments['ident'],
Buffet.recordMasa % 10 == 0,
))
SQL defines the mod
function so I used Sqlalchemy func.mod
(like func.lower
) with success on MySql. Also please note I’m using a data model class that inherits from Sqlalchemy’s db.Model
class in the context of Flask. The code to build the query that checks for rows with an even number in one column looks like this:
from sqlalchemy import func
qry = MyDataModel.query
qry = qry.filter(func.mod(MyDataModel.some_int_col, 2) == 0)
I have this raw SQL statement that I would like to use for both Core and ORM:
SELECT * FROM `buffet` WHERE `roomId` = '864495034004835' AND `recordMasa` > '1514600000' AND `recordMasa` < '1514900000' AND `recordMasa` mod 10 = 0 LIMIT 0,10000000000;
Please do let me know how can I add to my existing code below to include the modulo function:
select_statement = select([Buffet]).where(and_(
Buffet.recordMasa > arguments['startDate'],
Buffet.recordMasa < arguments['endDate'],
Buffet.roomId == arguments['ident']
))
rooms = conn.execute(select_statement).fetchall()
what about the modulo operator from python?
select_statement = select([Buffet]).where(and_(
Buffet.recordMasa > arguments['startDate'],
Buffet.recordMasa < arguments['endDate'],
Buffet.roomId == arguments['ident'],
Buffet.recordMasa % 10 == 0,
))
SQL defines the mod
function so I used Sqlalchemy func.mod
(like func.lower
) with success on MySql. Also please note I’m using a data model class that inherits from Sqlalchemy’s db.Model
class in the context of Flask. The code to build the query that checks for rows with an even number in one column looks like this:
from sqlalchemy import func
qry = MyDataModel.query
qry = qry.filter(func.mod(MyDataModel.some_int_col, 2) == 0)