Home > OS >  How to select string contains all substrings in SQL
How to select string contains all substrings in SQL

Time:02-22

I want to select row from table, which contains ONLY ALL substrings. For example, I have table CAR_DESCRIPTION that contains rows with values in column Desc like this:

tesla is super car
bmw is cool car
opel is budget car

And I want to select only the first row (tesla is super car) by specifying this substring (tesla, car), other rows no.

How to do it?

And I don't know how many words need to compare (it can be difference).

CodePudding user response:

You use LIKE with % as joker

create table cars(car varchar(50));
insert into cars values
('tesla is super car'),
('bmw is cool car'),
('opel is budget car');
select * from cars
where car like '%tesla%super%';

Returns

car  
Tesla is super car

If we write

where car like '%tesla%' and car like '%super%

The words can be in either order.
It's not clear what you are looking for the following example might help:

WHERE 
   (car LIKE '%tesla%' AND car like '%super%') 
      OR 
   (car LIKE '%mazda%' AND car like '%racing%') 

CodePudding user response:

You can use the like function, which returns all the rows where characters are similar to the ones you put in the quotation.

  • Related