簡體   English   中英

排序查詢以選擇匹配或部分匹配的名稱

[英]Sequelize query to select match or partially match name

我需要你的幫助。 我正在使用node.js和reactJS在Web服務器上工作。 我想為管理員創建一個名為“用戶”的頁面,以在SQL數據庫中搜索用戶。 我編寫了一個代碼,該代碼創建了兩個選項以按“名稱”或“國家”搜索用戶,這是代碼:

        UserSearch: {
            fields : {
        firstname: {value:'', type:'text', label:"Name", placeholder:'type name', bluredBefore:false, validators:['non'], errorReport:[]},
        country: {value:'-1', type:'select', label:"Country", placeholder:'select country', staticDataSource:'countries', bluredBefore:false, validators:['non'], errorReport:[]},
    },
  errorReport:false,
    isWaiting : false,
    queryTarget: 'userFind',
    queryView: 'UserSearchGrid',
    queryViewFields: 'userId, firstname, lastname ',
    formCols:3,
    notification: '',

},

UserSearchGrid: {searchForm:'UserSearch',
    viewFields: ['userId','firstname','lastname'],
    dataSource:[], count:0, offset:0, limit:10},

在查詢文件中,我編寫了以下代碼:

  var where = {userId:userId,};
        if (typeof args.firstname !== 'undefined' && args.firstname !== '')
            where['firstname'] = args.firstname;
        if (typeof args.country !== 'undefined' && args.country !== '-1')
              where['country'] = args.country;

        items = await UserProfile.findAndCountAll({

            where: where,  })

現在,這兩段代碼創建了一個文本框“名稱”和一個下拉菜單“國家/地區”,管理員必須選擇確切的用戶名和正確的國家/地區才能獲得結果。
我需要一個序列查詢,讓管理員輸入名稱(名字或姓氏)或國家/地區,並獲得與任何匹配名稱或部分匹配或與匹配國家/地區匹配的結果。

另外:我不知道是否有可能,我希望在管理員寫名字的過程中出現結果(匹配每個鍵入的字母並顯示結果)。 預先感謝任何人都可以提供幫助。

解決:我編輯了if語句,如下所示:

    var where = {};
    if (typeof args.firstname !== 'undefined' && args.firstname !== '')
    //  where['firstname'] = args.firstname;
    where['$or']={firstname:{$like: `%${args.firstname}%`},lastname:{$like: `%${args.firstname}%`}};



 items = await UserProfile.findAndCountAll({
              where: where ,  })

(其中)是包含子句(或)的對象,它會從管理員輸入的包含(args.firstname)的數據庫名字或姓氏中進行選擇-

例如:我想在數據庫中搜索用戶“ John”,如果我鍵入“ Jo”,它將從數據庫中以“ John”作為其姓或名的所有用戶獲得結果,因為他們的數據庫中包含“ Jo”名稱。

暫無
暫無

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

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