【问题标题】:Python: MySQL select null with non null dataPython:MySQL选择具有非空数据的空
【发布时间】:2017-10-06 13:31:24
【问题描述】:

我正在使用 Python Flask 和 MySQL。我从应用程序获取名称、价格和数量的输入,以便从 MySQL 搜索所有数据。

输出如下:

name   | Price | Volume
Screw  | 5.0   | 700
iron   | null  | 67
wood   | 23    | null
metal  | 76    | 56
plywood| 100   | null
rebar  | 75    | 59
steel  | null  | 87
L steel| 78    | 65

我需要的是,当我选择特定范围的交易量时,我想为交易量排除 null,但为价格包括 null,反之亦然。

场景 1:

选择 60 到 100 之间的交易量,选择所有形式的价格和任何名称。

当前输出为:

name   | Price | Volume
iron   | null  | 67
wood   | 23    | null
plywood| 100   | null
steel  | null  | 87
L steel| 78    | 65

我需要的输出:

name   | Price | Volume
iron   | null  | 67
steel  | null  | 87
L steel| 78    | 65

场景 2:

选择 50 到 120 之间的价格,选择所有形式的数量和任何名称。

电流输出:

name   | Price | Volume
iron   | null  | 67
metal  | 76    | 56
plywood| 100   | null
rebar  | 75    | 59
steel  | null  | 87
L steel| 78    | 65

我需要的输出:

name   | Price | Volume
metal  | 76    | 56
plywood| 100   | null
rebar  | 75    | 59
L steel| 78    | 65

下面是我的代码:

@app.route('/ABC/search1', methods=['GET'])
def ABCsearch1():
    name = request.args.get('name',default='',type=str)
    priceMin = request.args.get('priceMin',default='',type=str)
    priceMax = request.args.get('priceMax',default='',type=str)
    volMin = request.args.get('volMin',default='',type=str)
    volMax = request.args.get('volMax',default='',type=str)

        limit = request.args.get('limit',default=0,type=int)
    offSet = request.args.get('offSet',default=0,type=int)

    query = """ SELECT * FROM KLSE WHERE (Stock LIKE :s0 or Name LIKE :s1 or Number LIKE :s2)
                AND (Price BETWEEN (IF(:s3='_',-5000,:s4)) AND (IF(:s5='_',5000,:s6)) OR Price IS NULL)
                AND (Volume BETWEEN (IF(:s7='_',-5000,:s8)) AND (IF(:s9='_',5000,:s10)) OR Volume IS NULL)
                LIMIT :s95 OFFSET :s96 """
    query = text(query)
    input = {'s0':name+"%",'s1':name+"%",'s2':name+"%",'s3':priceMin,'s4':priceMin,'s5':priceMax,'s6':priceMax,'s7':volMin,'s8':volMin,'s9':volMax,'s10':volMax,

                's95':limit,'s96':offSet}
    try:
        call = db.session.execute(query,input)
        f = call.fetchall()
        col = ['index','Name','Number','Price','id']
        f1 = [OrderedDict(zip(col,t)) for t in f]
    except Exception:
        return 'Error'

    return jsonify({'Stock': f1})

【问题讨论】:

    标签: python mysql flask flask-sqlalchemy


    【解决方案1】:

    好的,如果我理解正确,您似乎想根据用户指定价格范围还是数量范围来发送不同的请求

    在这种情况下,最简单的解决方案是使用 if-else 语句来检查指定了哪个范围,并适当地修改查询字符串。

    例如,

    if (priceMin is None) and (priceMax is None): 
    # (Or whatever you want to compare it to)
        query = """blah blah"""
        input = blah blah
    
    else if (volMin is None) and (volMax is None):
        query = """blah blah"""
        input = blah blah
    
    # execute query...
    

    如果这有帮助,请告诉我。顺便说一句,我强烈建议您使用 SQLAlchemy 而不是像您所做的那样创建 SQL 查询字符串。这样方便多了!你应该看这里的教程:http://docs.sqlalchemy.org/en/rel_1_1/intro.html#documentation-overview

    【讨论】:

    • 这就是我想要的!但是,我认为我正在使用 sqlalchemy,您应该建议我做什么?
    • @bkcollection 抱歉回复晚了。 Sqlalchemy 无需您用 SQL 语法写出完整的查询字符串,而是只需使用查询命令,例如:items = session.query('Item').filter_by(blah blah blah).first()
    • 它不仅更方便,而且更稳定(避免错误)。现在,您的操作几乎与使用 Python DB-API 相同,包括为每个数据库操作编写完整的 SQL 语法。
    猜你喜欢
    • 2020-01-06
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2021-09-18
    • 2012-05-04
    相关资源
    最近更新 更多