简体   繁体   English

动态返回一列值等于另一列值的所有SQL结果

[英]Dynamically return all SQL results where the value of one column equals the value of another column

I've got a search form that looks through a table for employees.我有一个搜索表单,可以查看员工的表格。 I only want the employees that match the SupEmp to be returned in the search.我只希望在搜索中返回与 SupEmp 匹配的员工。 The person doing the search has an EmployeeNumber.执行搜索的人有一个 EmployeeNumber。 That employee number is dynamically imported when you sign into the search form.当您登录搜索表单时,该员工编号将动态导入。 If your EmployeeNumber matches other SupEmp numbers , then display all rows that match your EmployeeNumber.如果您的EmployeeNumber与其他SupEmp numbers匹配,则显示与您的 EmployeeNumber 匹配的所有行。

Currently my code just returns all employees.目前我的代码只返回所有员工。 The revised code should only show the employees whose SupEmp matched your EmployeeNumber.修改后的代码应该只显示 SupEmp 与您的 EmployeeNumber 匹配的员工。

var vendorCols = "SupEmp, FIRST_NAME, LAST_NAME, EmployeeNumber, DEPARTMENT, Position, Status";

       $('#EmployeeLookup').autocomplete({
           source: function (request, response) {
               var sql = "SELECT " + vendorCols + " FROM EMPLOYEE_SEARCH"
                   + " WHERE UPPER(FIRST_NAME + LAST_NAME) like '%" + request.term.toUpperCase() + "%'";

               $.ajax({
                   url: '/public/GetData',
                   type: 'POST',
                   data: JSON.stringify({ query: sql, connectionName: "LawsonConnection" }),
                   contentType: "application/json; charset=utf-8",
                   dataType: 'json',
                   success: function (dataArray) {
                       response($.map(dataArray, function (item) {
                           if (request.term.indexOf("'") >= 0) { item = 'Do not enter special characters' }
                           if (item === "Data Not Found")
                               item = "Employee Cannot Be Found";
                           return {
                               label: item
                           };
                       }));
                   }
               });
           },

There's a lot of blanks on your question, but from what I understood, you need to add an AND clause to your query and considering you have a list SupEmp numbers , it should be something like:您的问题有很多空白,但据我所知,您需要在查询中添加一个 AND 子句,并考虑到您有一个列表SupEmp numbers ,它应该是这样的:

+ " WHERE UPPER(FIRST_NAME + LAST_NAME) like '%" + request.term.toUpperCase() + "%'"
+ " AND EmployeeNumber IN (" +request.SupEmpNumbers,.join(', ') + ")" ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM