Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Combining multiple like queries

Tags:

sql

postgresql

Hey my first question on SO! Anywho...

Still relatively a newb at SQL so I think I might be missing something here. My question is I currently have a table full of phone numbers. I want to have a query where I search for phone numbers that are similar to a list I have. So for example, I want to find phone numbers that begin with '555123', '555321', and '555987'. I know normally if you have a list of numbers you could just do a query such as

SELECT *    FROM phonenumbers   WHERE number in ('5551234567', '5559876543', .... ); 

Is there a way to do this with like? Such as

SELECT *    FROM phonenumbers   WHERE number in like ('555123%', '555321%', '555987%'); //I know this doesn't actually work 

Instead of have to do this individually

SELECT *    FROM phonenumbers   WHERE number like '555123%'      or number like '555321%'      or number like '555987%'; //Which does work but takes a long time 

Or is there an easier to do this that I'm just missing? I'm using postgres, I don't know if there's any commands it has that would help out with that. Thanks!

like image 527
The Jug Avatar asked Feb 11 '10 15:02

The Jug


People also ask

How do you do multiple like conditions in SQL?

Using the LIKE operator, you can specify single or multiple conditions. This allows you to perform an action such as select, delete, and updating any columns or records that match the specified conditions. It is mainly paired with a where clause to set the conditions.

Can we use multiple like in SQL?

No, MSSQL doesn't allow such queries.

How do you do multiple conditions in like?

select * from employee where name like '%aa%' or name like '%bb% or name like '%cc%'..... Next row NAME Column Contains aa bb cc dd..... each value in the NAME column and put in the multiple like operator using OR condition.

How do I match multiple values in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);


1 Answers

You can use SIMILAR TO and separate the tags with | pipe '555123%|555321%|555987%'

eg:

SELECT *  FROM phonenumbers  WHERE number SIMILAR TO '555123%|555321%|555987%' 
like image 190
Pentium10 Avatar answered Sep 21 '22 16:09

Pentium10