How do I search for dates registered as an array in Json?

自古美人都是妖i 提交于 2020-01-05 07:09:10

问题


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:

  1. Search for a filter value in an array in a column made up of json.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!