how to use LIKE with column name

后端 未结 6 1948
刺人心
刺人心 2020-12-05 04:09

Normally LIKE statement is used to check the pattern like data.

example:

select * from table1 where name like \'ar%\'

相关标签:
6条回答
  • 2020-12-05 04:20

    SQL SERVER

     WHERE ColumnName LIKE '%'+ColumnName+'%'
    
    0 讨论(0)
  • 2020-12-05 04:22
    declare @LkeVal as Varchar(100)
    declare @LkeSelect Varchar(100)
    
    Set @LkeSelect = (select top 1 <column> from <table> where <column> = 'value')
    Set @LkeVal = '%' + @LkeSelect
    
    select * from <table2> where <column2> like(''+@LkeVal+'');
    
    0 讨论(0)
  • 2020-12-05 04:32
    ...
    WHERE table1.x LIKE table2.y + '%'
    
    0 讨论(0)
  • 2020-12-05 04:33

    ORACLE DATABASE example:

    select * 
    from table1 t1, table2 t2
    WHERE t1.a like ('%' || t2.b || '%')
    
    0 讨论(0)
  • 2020-12-05 04:35

    You're close.

    The LIKE operator works with strings (CHAR, NVARCHAR, etc). so you need to concattenate the '%' symbol to the string...


    MS SQL Server:

    SELECT * FROM table1,table2 WHERE table1.x LIKE table2.y + '%'
    


    Use of LIKE, however, is often slower than other operations. It's useful, powerful, flexible, but has performance considerations. I'll leave those for another topic though :)


    EDIT:

    I don't use MySQL, but this may work...

    SELECT * FROM table1,table2 WHERE table1.x LIKE CONCAT(table2.y, '%')
    
    0 讨论(0)
  • 2020-12-05 04:43

    For SQLLite you will need to concat the strings

    select * from list1 l, list2 ll 
    WHERE l.name like "%"||ll.alias||"%";
    
    0 讨论(0)
提交回复
热议问题