What is the most efficient and elegant SQL query looking for a string containing the words "David", "Moses" and "Robi". Assume the table is named T and the column C.
-
Depends on the SQL Dialect. Which server?Brian Webster– Brian Webster2011-06-28 12:44:33 +00:00Commented Jun 28, 2011 at 12:44
-
Are you looking to match all three words in the same field or just return all rows that match any one of the three? I read it as matching all, but it looks like many people interpreted it as any.Wiseguy– Wiseguy2011-06-28 13:09:03 +00:00Commented Jun 28, 2011 at 13:09
-
What RDBMS? Do you want to know if the same row in the column contains all three, or just that the column itself contains all 3 on some number of rows? What's the structure of the data, are they single names or do we need to account for additional string data? What's the actual data type? Do we need to worry about foreign language characters as well as latin? Someone who has asked 34 questions probably should know what information to provide.JNK– JNK2011-06-28 13:14:18 +00:00Commented Jun 28, 2011 at 13:14
-
Thank you all. For some reason the exact same wording with no clarifications sufficed for a friend of mine who's a DBA to provide me my answer. Thank you for you trouble. @Wiseguy - My original wording was exact, but @hamlin11 edited it, possibly making it unclear as you said. Though I'm sure he meant well.user181218– user1812182011-06-28 13:21:21 +00:00Commented Jun 28, 2011 at 13:21
9 Answers
Select * from table where
columnname like'%David%' and
columnname like '%Moses%' and columnname like'%Robi%'
3 Comments
select * from T where C like'%David%Moses%Robi%'In SQL Server 2005+ with Full-Text indexing switched on, I'd do the following:
SELECT *
FROM T
WHERE CONTAINS(C, '"David" OR "Robi" OR "Moses"');
If you wanted your search to bring back results where the result is prefixed with David, Robi or Moses you could do:
SELECT *
FROM T
WHERE CONTAINS(C, '"David*" OR "Robi*" OR "Moses*"');
2 Comments
ANDs instead of ORs -- which also means that multiple prefixes wouldn't make sense, but thanks for adding that info.Here is what I uses to search for multiple words in multiple columns - SQL server
Hope my answer help someone :) Thanks
declare @searchTrm varchar(MAX)='one two three ddd 20 30 comment';
--select value from STRING_SPLIT(@searchTrm, ' ') where trim(value)<>''
select * from Bols
WHERE EXISTS (SELECT value
FROM STRING_SPLIT(@searchTrm, ' ')
WHERE
trim(value)<>''
and(
BolNumber like '%'+ value+'%'
or UserComment like '%'+ value+'%'
or RequesterId like '%'+ value+'%' )
)
Comments
Oracle SQL :
select *
from MY_TABLE
where REGEXP_LIKE (company , 'Microsodt industry | goglge auto car | oracles database')
- company - is the database column name.
- results - this SQL will show you if company column rows contain one of those companies (OR phrase) please note that : no wild characters are needed, it's built in.
more info at : http://www.techonthenet.com/oracle/regexp_like.php
Comments
if you put all the searched words in a temporaray table say @tmp and column col1, then you could try this:
Select * from T where C like (Select '%'+col1+'%' from @temp);
3 Comments
PostgreSQL full text search example
To implement this search efficiently, you will need the full text search feature which is present in many RDMSs. Here is how you'd do it on PostgreSQL. Create the indexed table:
CREATE TABLE fts(names TEXT, age INTEGER);
ALTER TABLE fts ADD COLUMN names_ts tsvector
GENERATED ALWAYS AS (to_tsvector('english', names)) STORED;
EOF
CREATE INDEX names_ts_gin_idx ON fts USING GIN (names_ts);
And finally you can query with:
SELECT * FROM fts WHERE names_ts @@ to_tsquery('english', 'David & Moses & Robi');
Documented at: https://www.postgresql.org/docs/17/gin.html
Tested on PostgreSQL 16.6, Ubuntu 24.10.
Comments
Oracle SQL:
There is the "IN" Operator in Oracle SQL which can be used for that:
select
namet.customerfirstname, addrt.city, addrt.postalcode
from schemax.nametable namet
join schemax.addresstable addrt on addrt.adtid = namet.natadtid
where namet.customerfirstname in ('David', 'Moses', 'Robi');