在多对一个临时表查询中选择值,例如名称与其他名称类似的值

时间:2019-04-17 19:58:34

标签: sql mariadb

我正在尝试为资产跟踪工具建立一个新的搜索参数,并且正在使用两个临时表进行连接和查询,因为资产可能具有多个我需要的软件,而有条件的条件只会找到具有..的软件。它们中的.value附加到任何名称,但过滤掉不再匹配的潜在匹配项:在这种情况下为software.I知道我必须做错了什么,但我只是看不到...下面是用于生成临时表的代码,稍后在搜索中使用...

CREATE TEMPORARY TABLE lookup_tbl_2 SELECT am_software.id,
            am_software.asset_name,
            am_software.sw_name,
            am_software.sw_key,
            am_software.sw_osver
        FROM am_software
        UNION ALL
        SELECT am_software_archive.id,
            am_software_archive.asset_name,
            am_software_archive.sw_name,
            am_software_archive.sw_key,
            am_software_archive.sw_osver
        FROM am_software_archive;

CREATE TEMPORARY TABLE lookup_tbl_1 SELECT am_assets.id,
            am_assets.asset_name,
            am_assets.asset_family,
            am_assets.asset_type,
            am_assets.asset_location,
            am_assets.asset_manufacturer,
            am_assets.asset_model,
            am_assets.asset_serial,
            am_assets.asset_status,
            am_assets.asset_retired_on,
            am_networks.connection_type,
            CASE WHEN am_networks.ipa_pointer = 1 THEN 'Dynamic' ELSE CONCAT_WS('.', am_ip_addresses.ip_address, am_networks.ip_address) END AS 'display_address'
        FROM am_assets
        JOIN am_networks ON am_assets.asset_name = am_networks.asset_name
        JOIN am_locations ON am_assets.asset_location = am_locations.id
        JOIN am_asset_family ON am_assets.asset_family = am_asset_family.id
        JOIN am_asset_type ON am_assets.asset_type = am_asset_type.id
        JOIN am_ip_addresses ON am_networks.ipa_pointer = am_ip_addresses.id
        JOIN am_connection_types ON am_networks.connection_type = am_connection_types.id
        UNION ALL
        SELECT am_asset_archive.id,
            am_asset_archive.asset_name,
            am_asset_archive.asset_family,
            am_asset_archive.asset_type,
            am_asset_archive.asset_location,
            am_asset_archive.asset_manufacturer,
            am_asset_archive.asset_model,
            am_asset_archive.asset_serial,
            am_asset_archive.asset_status,
            am_asset_archive.asset_retired_on,
            am_network_archive.connection_type,
            CASE WHEN am_network_archive.ipa_pointer = 1 THEN 'Dynamic' ELSE CONCAT_WS('.', am_ip_addresses.ip_address, am_network_archive.ip_address) END AS 'display_address'
        FROM am_asset_archive
        JOIN am_network_archive ON am_asset_archive.asset_name = am_network_archive.asset_name
        JOIN am_locations ON am_asset_archive.asset_location = am_locations.id
        JOIN am_asset_family ON am_asset_archive.asset_family = am_asset_family.id
        JOIN am_asset_type ON am_asset_archive.asset_type = am_asset_type.id
        JOIN am_ip_addresses ON am_network_archive.ipa_pointer = am_ip_addresses.id
        JOIN am_connection_types ON am_network_archive.connection_type = am_connection_types.id;

同样,目标是搜索临时表并将值返回给ui;所以这就是我遇到麻烦的地方:

    SELECT lookup_tbl_1.asset_name as 'asset_name' 
    FROM lookup_tbl_1 
    JOIN lookup_tbl_2 
    ON lookup_tbl_1.asset_name = lookup_tbl_2.asset_name 
    WHERE lookup_tbl_2.sw_name LIKE 'Office 2010' AND lookup_tbl_2.sw_name LIKE 'Atom'
    AND lookup_tbl_1.asset_location = 5;

软件名称是文本,不必具有类似名称,即使在使用时也是如此:

    SELECT lookup_tbl_1.asset_name as 'asset_name' 
    FROM lookup_tbl_1 
    JOIN lookup_tbl_2 
    ON lookup_tbl_1.asset_name = lookup_tbl_2.asset_name 
    WHERE lookup_tbl_2.sw_name = 'Office 2010' 
    AND lookup_tbl_2.sw_name = 'Atom'
    AND lookup_tbl_1.asset_location = 5;

已知这些值与我要显示的资产名称相关联的特定资产,如果我删除软件名称条件之一(sw_name),则有效,但是当存在多个以上时,则无效...

我一直在寻找可行的解决方案,并且尝试过类似的事情:

WHERE lookup_tbl_2.sw_name LIKE 'Office 2010' AND 'Atom'
WHERE lookup_tbl_2.sw_name LIKE 'Office 2010' 'Atom'
WHERE lookup_tbl_2.sw_name = 'Office 2010' AND 'Atom'
WHERE (find_in_set('Office 2010', lookup_tbl_2.sw_name)>0 AND find_in_set('Atom', lookup_tbl_2.sw_name)>0)

全部返回相同的空结果,但是删除第二个sw_name条件的效果很好...

预期:应返回资产名称列表,例如“ JWW90120”(实际资产名称值应与所有条件匹配,因此应包含在预期列表中)。

实际:空结果。

1 个答案:

答案 0 :(得分:0)

我从一位同事那里得到了一些帮助,尽管在解决了一些错误之后我们帮助了我,但最终我们使它开始工作。

工作代码:

  class LDAP {

    public function connect($host, $user, $pass){
      $ds = ldap_connect($host);     
      if(!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)){
        print "Could not set LDAPv3";
      } else {
          $ldap = ldap_bind($ds, $user, $pass);
        }
      if(strpos($host, 'ldaps://') !== false){
        $ssl = ' over SSL';
        $host = str_replace('ldaps://', '', $host);
      } else {
          $ssl = null;
          $host = str_replace('ldap://', '', $host);
        }

      if($ldap) {
        //$host = str_replace('ldap://', replace, subject)
        echo '<b>LDAP</b> : <u>Microsoft AD</u> <br /><br />
          Connection to <u>' . $host . '</u>' . $ssl . ' was successful! <br /><br />
          [WebServer] ←→ [LDAP Server] <br /><br />
          <b>Status:</b> <u>Up</u> &#10004; <br />';
      } else {
          echo 'Connection to <u>' . $host . '</u>' . $ssl . ' was NOT successful. Please try again. <br /><br />
          [WebServer] ←x→ [LDAP Server] <br /><br />
          <b>Status:</b> <u>Down</u> &#10006; <br />';
        }
    }

    public function disconnect(){
      $ldap = null;
    }
  } # class ldap

  $LDAP = new LDAP();
  $host = "ldaps://dc.domain.com";
  $user  = "svc.ldap@domain.com";
  $pass = "password1";
  $LDAP->connect($host, $user, $pass);
  $LDAP->disconnect();
  // echo 'HOST['.$host.'] USER['.$user.']'; // toggle to troubleshoot db connection

此问题已解决...希望对将来可能会遇到这种情况的其他人有所帮助。