簡體   English   中英

NodeJS MySQL:第二個參數為空時查詢

[英]NodeJS MySQL: Query when second parameter is null

當第二個或任何下一個參數但第一個為null時,是否可以完成mySQL查詢?

例如,我基於給定日期( datereport )為單個項目( itemreport )生成報告。

現在,當沒有選擇任何項目到前端並且其req.query.itemreport null ,必須考慮用戶要求所有項目而不是單個請求而不停止執行來執行其余查詢,因為它獲取了null參數( itemreport )。

reportsdateres.post(function(req, res, next) {

    datereport = req.query.datereport;
    itemreport = req.query.itemreport; // this can be null for querying all items

    req.getConnection(function(err, conn) {

        if (err) return next("Cannot Connect");

        var query = conn.query("SELECT SUM(total_profit) AS fee_total FROM transactions WHERE date_created = ? AND item = ?", [datereport, itemreport], function(err, rows) {
        //                                                                                                     ^^^^^^^^^^^^ 

            // do myStuff

        });
    });
});
reportsdateres.post(function(req, res, next) {

    datereport = req.query.datereport;
    itemreport = req.query.itemreport; // this can be null for querying all items

    req.getConnection(function(err, conn) {

        if (err) return next("Cannot Connect");

       var queryString = "SELECT SUM(total_profit) AS fee_total FROM transactions WHERE date_created = ? ";
       var queryParams = [];
       queryParams.push(datereport);

       if(itemreport){
          queryString += ' AND item = ?'
          queryParams.push(itemreport);
        }

        var query = conn.query(queryString, queryParams, function(err, rows) {
          // do yourStuff

        });
    });
});

更新資料

添加了修訂,添加了查詢字符串。 甚至我錯過了它:)

reportsdateres.post(function(req, res, next) {

datereport = req.query.datereport;
itemreport = req.query.itemreport; // this can be null for querying all items

req.getConnection(function(err, conn) {

    if (err) return next("Cannot Connect");
    if (itemreport==null || itemreport=='') {
        var sql = "SELECT SUM(total_profit) AS fee_total FROM transactions WHERE date_created = ?";
        var params = [datereport];
    }else{
        var sql = "SELECT SUM(total_profit) AS fee_total FROM transactions WHERE date_created = ? AND item = ?";
        var params = [datereport, itemreport];
    }
    var query = conn.query(sql, params, function(err, rows) {
    //                                                                                                     ^^^^^^^^^^^^ 

        // do myStuff

    });
}); });
reportsdateres.post(function(req, res, next) {

    datereport = req.query.datereport;
    itemreport = req.query.itemreport; // this can be null for querying all items

    req.getConnection(function(err, conn) {

        if (err) return next("Cannot Connect");

        var query = conn.query("SELECT SUM(total_profit) AS fee_total FROM transactions WHERE date_created = ? AND item = COALESCE(?, item)", [datereport, itemreport], function(err, rows) {

            // do myStuff
        });
    });
});

COALESCE(如果代碼中沒有其他代碼,我們可以避免使用)如果不為null,它將考慮第一個參數,否則它將item列的值與其自身進行比較,有關更多詳細信息,請訪問: https : //www.w3schools.com /sql/sql_isnull.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM