Select statment from multiple tables, with variable input

前端 未结 3 1704
自闭症患者
自闭症患者 2021-01-16 09:17

I have two tables: AreaCode and EquipmentNumber.

+------------------------------------+
| AreaCd                             |
|---         


        
3条回答
  •  旧巷少年郎
    2021-01-16 09:43

    I think this is what you are after :

    Just added a simple trick;

    if(string.IsNullOrEmpty(sNumber))
        sNumber="$$$";               //If sNumber is empty, then selection using sNumber= '$$$' will return nothing.
    if(string.IsNullOrEmpty(sType))
        sType="$$$"                 //If sType is empty, then selection using sType = '$$$' will return nothing.
    "SELECT Number, Type, Code FROM EqNum, AreaCd " +
    "WHERE EqNum.AreaId = AreaCd.AreaId 
    AND 
      (Code = " + int nCode + " 
       OR Number = '" + string sNumber + "' 
       OR Type = '" + string sType + "')"
    

    OR using LIKE:

    if(string.IsNullOrEmpty(sNumber))
        sNumber="$$$";             //If sNumber is empty, then selection using sNumber LIKE '%$$$%' will return nothing.
    if(string.IsNullOrEmpty(sType))
        sType="$$$"                //If sType is empty, then selection using sType LIKE '%$$$%' will return nothing.
    "SELECT Number, Type, Code FROM EqNum, AreaCd " +
    "WHERE EqNum.AreaId = AreaCd.AreaId 
    AND 
      (Code = " + int nCode + "
       OR Number LIKE '%" + string sNumber + "%' 
       OR Type LIKE '%" + string sType + "%')"
    

    See an example at SQL Fiddle

提交回复
热议问题