【问题标题】:SQL Like with a subquerySQL Like 子查询
【发布时间】:2013-06-15 04:38:01
【问题描述】:

我怎样才能做到这一点?

SELECT * 
FROM   item 
WHERE  item_name LIKE '%' 
                      || (SELECT equipment_type 
                          FROM   equipment_type 
                          GROUP  BY equipment_type) 
                      || '%' 

内部子查询返回一个字符串列表,如“The”、“test”、“another”,我想从 item_name 类似于子查询返回值的项目表中选择所有项目。我需要外卡。

我可以使用通配符但使用 IN sql 命令代替吗?

【问题讨论】:

  • 你没有身份证吗??
  • @TechBytes 你是什么意思?
  • @TechBytes 我特别声明我有一个字符串列表我想比较 item_name 。

标签: sql subquery wildcard


【解决方案1】:

您可以使用INNER JOIN

SELECT I.* 
FROM item I
INNER JOIN (SELECT equipment_type 
            FROM equipment_type 
            GROUP BY equipment_type) E
    ON I.item_name LIKE '%' || E.equipment_type || '%'

【讨论】:

  • 啊,不错不错,我会试试这个,并尽快让你知道。 ty
  • 对于那些使用 SQL Server 的人... 双管道似乎是 ANSI-SQL 标准,但在 T-SQL 中不起作用。 stackoverflow.com/questions/23372550/…
  • 请注意,此解决方案会产生重复。
【解决方案2】:

如果你不想担心重复并且不在乎哪一个匹配,那么切换到使用exists

select i.*
from item i
where exists (select 1
              from equipment_type
              where i.item_name like '%'||equipment_type||'%'
             )

【讨论】:

    【解决方案3】:

    对于上面的 MSSql Server 不飞

    使用

    select *
    from item I
    where exists (select 1
              from equipment_type
              where i.item_name like (SELECT CONCAT('%',equipment_type,'%')) 
             )
    

    【讨论】:

      【解决方案4】:

      您可以使用CONCAT 并插入子查询:

      SELECT * FROM item WHERE  item_name LIKE  
      CONCAT('%', (
          SELECT equipment_type
          FROM equipment_type
          GROUP BY equipment_type), '%'
      )
      

      【讨论】:

        猜你喜欢
        • 2021-04-12
        • 1970-01-01
        • 1970-01-01
        • 2020-09-13
        • 2015-09-02
        • 1970-01-01
        • 2018-09-18
        • 2017-08-15
        • 2016-06-29
        相关资源
        最近更新 更多