Interval MySQL
See original GitHub issuePypika messes up Interval dates when using MySQL queries:
from pypika import functions as fn
fruits = Tables('fruits')
q = MySQLQuery.from_(fruits) \
.select(fruits.id, fruits.name) \
.where(fruits.harvest_date + Interval(months=1) < fn.Now())
str(q)
SELECT `fruits`.`id`,`fruits`.`name` FROM `fruits` JOIN `consumers` ON `fruits`.`consumer_id`=`consumers`.`id` WHERE `fruits`.`date`>=NOW()-INTERVAL `1 DAY`
I believe that the problem is in line 1080 of terms.py:
return self.templates.get(dialect, 'INTERVAL \'{expr} {unit}\'')
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
An Introduction To MySQL Interval
This tutorial shows you step by step how to use the MySQL interval for date and time arithmetic with many practical examples.
Read more >MySQL INTERVAL() function - w3resource
MySQL INTERVAL () function returns the index of the argument that is more than the first argument. ... It returns 0 if 1st...
Read more >MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions
12.7 Date and Time Functions ; DATE(), Extract the date part of a date or datetime expression ; DATE_ADD(), Add time values (intervals)...
Read more >MySQL Interval - Javatpoint
MySQL interval is an operator, which is based on the binary search algorithm to search the items and returns the value from 0...
Read more >Working with INTERVAL and CURDATE in MySQL
I'm building a chart and I want to receive data for each month. Here's my first request which is working: SELECT s.GSP_nom AS...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Result:
Expected result:
I also noticed that the desired result is showed if
str(q)
is used instead ofq.get_sql()
.I’m having the same issue with using
Interval
only usingstr(q)
doesn’t solve my problem as I’m generating SQL for GCP BigQuery which requires me to remove all quote chars withq.get_sql(quote_char=None)
FromInterval(quarters=1)
I’m looking to generateINTERVAL 1 QUARTER
instead ofINTERVAL '1 QUARTER'
I can’t do a simple
query_str.replace("'", str())
because my query containsCAST('inf' AS FLOAT64)
So my current solution is some regex to strip the'
specifically fromINTERVAL
statementsre.sub("(INTERVAL '[0-9]+ [A-Z]\w+')", lambda x:x.group(0).replace("'", str()), query_str)