Home > OS >  How to combinate LIKE IN in SQL or JPA
How to combinate LIKE IN in SQL or JPA

Time:03-07

For example I have this strings:

Tesla is good car
Tesla is electric car
Opel is budget auto

And by words: opel, tesla I want to select all this strings.

CodePudding user response:

SIMILAR TO would work

CREATE TABLE tab1 (
  "id" INTEGER,
  "mytext" VARCHAR(21)
);

INSERT INTO tab1
  ("id", "mytext")
VALUES
  ('1', 'Tesla is good car'),
  ('2', 'Tesla is electric car'),
  ('4', 'Opel is budget auto');
SELECT * from tab1 WHERE mytext SIMILAR TO '(Opel|Tesla|VW)%';
id | mytext               
-: | :--------------------
 1 | Tesla is good car    
 2 | Tesla is electric car
 4 | Opel is budget auto  

db<>fiddle here

CodePudding user response:

Can't you just use this if your only two strings are 'Opel' and 'Tesla'?

Under the assumption that all your data looks the way you represented it in your post:

SELECT *
FROM your_table
WHERE your_field LIKE 'Tesla%' 
OR your_field LIKE 'Opel%'
  • Related