问题
How do I search for dates registered as an array in Json? (PostgreSQL is DB.) Below is the relevant code.
##### 1. Model #####
class Business(db.Model):
__tablename__ = 'business'
id = db.Column(db.Integer, primary_key=True)
sales = db.Column(JSON)
##### 2. Data #####
id=1
sales={'time': ['10:00', '19:00']},
##### 3. View Query #####
# filter var
filter_val = request.json['filter']['value']
# basic query
bQuery = Business.query
# Filter query
if filter_val['sales']['time'][0]:
bQuery = bQuery.filter(Business.sales['time'][0].astext.ilike('%' + filter_val['sales']['time'][0] + '%'))
if filter_val['sales']['time'][1]:
bQuery = bQuery.filter(Business.sales['time'][1].astext.ilike('%' + filter_val['sales']['time'][1] + '%'))
# result query
business = bQuery.order_by(Business.id.desc()).paginate(pageCurrent, per_page=per_page, error_out=False)
Filter value: filter_val ['sales'] ['time'] [n]
Json array of table columns: business.sales ['time'] [n]
I have created a filter query in the view area,
filter_val ['sales'] ['time'] [0] ~[1]
Business.sales ['time'] [0] ~ [1] must be exactly the same, but they will be searched.
However, this is not the date search I want.
Here's what I want:
- Search for a filter value in an array in a column made up of json.
- The desired filter search condition is
- Business.sales ['time'] [0] > = filter_val ['sales'] ['time'] [0]
- Business.sales [ 'sales'] ['time'] [1] <= filter_val['sales']['time'][1]
Is there a good way? I want to know a simple example. Thank you.
来源:https://stackoverflow.com/questions/51697947/how-do-i-retrieve-the-date-registered-as-an-array-of-json