2017-05-08 79 views
1

我正在使用Python Flask和MySQL。我從應用程序獲得名稱,價格和音量的輸入以便從MySQL搜索到所有數據。Python:MySQL使用非null數據選擇null

enter image description here

的輸出如下:

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 

我需要的是什麼,當我與一個特定範圍音量選擇,我想排除空卷,但包含空值價格和反之亦然。

方案1:

60之間選擇音量爲100,選擇所有形式的價格和任何名稱。 enter image description here

電流輸出是:

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,選擇所有形式音量和任何名稱。

enter image description here

電流輸出:

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}) 

回答

0

好吧,如果我沒有理解CORRE ctly,似乎你想發送一個不同的請求根據用戶是否指定價格範圍或音量範圍。

在這種情況下,最簡單的辦法是使用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

+0

這就是我想要的!但是,我認爲我正在使用sqlalchemy,你應該建議我應該怎麼做? – bkcollection

+0

@bkcollection對於遲到的回覆感到抱歉。 SQLAlchemy中不再需要爲你的SQL語法寫出完整的查詢字符串,而不是你可以簡單的使用查詢命令,例如: '項目= session.query(「項目」)filter_by(等等等等等等)。首先() ' – zeo

+0

它不僅更方便,也更穩定(安全距離誤差)。現在你幾乎做同樣的使用Python DB-API,它涉及到每一個數據庫操作寫出完整的SQL語法。 – zeo