Search code examples
mysqlsqlspecial-characterssql-like

working with special character forward slash '\' or backward slash '/' in MySQL


I am storing the full-path details of the file in a table with its name and Id assigned to it. Now for retrieving the row on the query below

select *
     from path_table
     where path = 'D:\\\WorkBench\\\Demo\\\sample.txt'

This is working fine .

How can I get the same result with 'like' in MySQL

select *
     from path_table
     where path like 'D:\\\WorkBench\\Demo\\\sample.txt'

This statement returns 0 rows. Actually I need something like if I give

select *
     from path_table
     where path like 'D:\\\WorkBench\\\Demo\\\'

It should return all rows I.e. files under same path so need to use like with '%'. But its not working. What should I use instead?


Solution

  • try this query:

    select * from path_table where path like '%D:\\WorkBench\\Demo\\%'
    

    You forgot to put this sign (%) in the starting and ending.

    note:I put % sign in the starting also because may be there were some characters added in the db.