查询以三个键与join查询,where和like子句一起搜索

时间:2018-07-06 05:44:21

标签: join sql-like

我正在后端进行搜索操作,所以我尝试了一个查询。我的要求是用户可以使用(ip,instance name,vm name)进行搜索-如果他/她进行搜索,则可以使用这三个中的任何一个,他们应该获得虚拟机名称。这些名称与4个表有关,我的查询是:

 <pre>select cim.name as vm 
from ci_master cim 
     join
     ci_attribute_values  cav on cim.id = cav.ci_master_id
     join 
     ci_attributes cia on cav.ci_attributes_id=cia.id 
     join
     ci_category_master ccm on cim.ci_category_master_id=ccm.id 
where ccm.name='Virtual Machine'
  and ((cia.name='IP') or (cia.name='Instance Name'))
  and cim.name like 'comm%';

select distinct concat(cim.name,',',cav.ci_attribute_value) as vm
from ci_master cim 
     join
     ci_attribute_values  cav on cim.id = cav.ci_master_id 
     join
     ci_attributes cia on cav.ci_attributes_id=cia.id 
     join
     ci_category_master ccm on cim.ci_category_master_id=ccm.id 
where ccm.name='Virtual Machine' 
  and cia.name in ('IP','Instance Name')
  and ((cim.name) or (cav.ci_attribute_value) like '%comm%');

select distinct concat(cim.name,',',cav.ci_attribute_value) as vm 
  from ci_master cim 
       join
       ci_attribute_values  cav on cim.id = cav.ci_master_id 
       join
       ci_attributes cia on cav.ci_attributes_id=cia.id 
       join
       ci_category_master ccm on cim.ci_category_master_id=ccm.id 
where ccm.name='Virtual Machine'
  and cia.name in ('IP','Instance Name')
  and ((cim.name) or (cav.ci_attribute_value) like '%comm%');
</pre>

我已经尝试了所有这些查询,但是没有找到适当的解决方案。

0 个答案:

没有答案