sql select where string matches pattern defined in another table - mysql

I have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)

Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch

You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.

Related

Retrieve any name starting with letters 'Ai' in SQL

All I want to do is retrieve the names of the people who start with "ma".
Eg Matt, Mathers, Mac.
First, two letters have to be 'ma'.
Have a look at string comparisons functions.
SELECT *
FROM YOUR_TABLE
WHERE NAME LIKE 'MA%';
SELECT *
FROM YOUR_TABLE
WHERE NAME LIKE 'MA%'
There can be spaces at the beginning of the name just like ' Matthew'. This would fix it:
SELECT *
FROM YOUR_TABLE
WHERE ltrim(NAME) LIKE 'MA%'

how do i write a sql query to select the names that may contain any one of the vowels?

I m trying to query a database with about 2000 entries. I want to select the entries in which the names may contain any one of the vowel.
I tried using the following query, but it gives me those entries that contain all the given characters in that order.
select * from myTable where name like '%a%e%i%';
How do I modify the above query to select those entries with names that may contain at least anyone of the vowels.
Try this for SQL Server:
SELECT * FROM myTable WHERE name LIKE '%[AEIOU]%';
I hope this helps you...
SELECT * FROM myTable WHERE name REGEXP 'a|e';
or.....
SELECT * FROM myTable WHERE name REGEXP 'a|e|i';
In SQL Server, you would do:
where name like '%[aeiou]%';
In MySQL, you would do something similar with a regular expression.
Use OR like this.
This will work for both SQL Server and MySql.
select * from myTable where name like '%a%' OR name like '%e%' OR name like '%i%';
Use LIKE and OR.
Query
select * from myTable
where name like '%a%'
or name like '%e%'
or name like '%i%'
or name like '%o%'
or name like '%u%'

Checking if a multi string is found in one of multiple columns in mySQL

i need to find multi string in multi columns in mysql.
select * from myTable WHERE name LIKE '%stack%';
This obviously works. However, what I need to do is that if both name and surname are checked multi string would do something like this:
select * from myTable WHERE (name or surname) LIKE '%stack%','%over%','%flow%';
if similar above code that work does not exist. please tell me how can checking multi string in a column in mySQL with like statement:
select * from myTable WHERE name LIKE '%stack%','%over%','%flow%';
You can keep it simple and do:
select * from myTable WHERE name LIKE '%stack%' OR
name LIKE '%over%' OR
name LIKE '%flow%';
or:
select * from myTable WHERE name LIKE '%stack%' AND
name LIKE '%over%' AND
name LIKE '%flow%';
This doesn't look as nice as the REGEXP from Young, but it doesn't have the unexpected behaviour REGEXP can have given that user input can be anything. With REGEXP you really need to know what you're doing.
You can try:
select * from myTable WHERE name REGEXP 'stack|over|flow';
Though it's not a like solution.

how to know the count of words starting with a particular word in database?

I am using a mysql database for storing the data. The table consists of 2 columns s_no and name. I used index on this table on name. Now I want to get only words starting with a particular letter say "a" or "b" etc. How can I do it? Is there any SQL query for retrieving data in this fashion?
select s_no,name from tablename where name like 'a%' or name like 'b%'
select * from table_name
where name like 'a%'
select s_no,name from table where name like 'a%' or name like 'b%'
Select * from tablename where name like 'a%'
You can also pass the value inside parameters
#value varchar(50)
Select * from tablename where name like #value
so whatever characters u insert like a or b just mearge it with % and here you go
like #value=#value+ '%' before query .

mysql: need code for search data

i need some code for search some data inside DB...
i have two fields that is id and name...
if i type "ABC" it can show all data about "ABC"..
if "DEF"=>all DEF data...or i type anything it can show that "anything"...
i'm just know how to show all data inside fields that makes "ABC" and "DEF" show together,the code like this:
"SELECT ID,Name FROM Regist ORDER BY date";
MySQL wildcards: _ AND % (in conjunction with keyword LIKE)
_ denotes a single character
SELECT name FROM colors WHERE name LIKE 'gr_y';
Output: 'grAy', 'grEy'.
% denotes any number of characters
SELECT name FROM colors WHERE name LIKE 'gr%';
Output: 'grAY', 'grEY', 'grEEN'.
You can use both _ and % multiple times
SELECT name FROM color WHERE name LIKE '_ra%';
Output: 'GraY', 'OraNGE'.
In your case, if you want to list the names containing starting with either 'ABC' or 'DEF', your query would be like this:
SELECT id,name FROM Regist
WHERE name LIKE 'ABC%' OR name LIKE 'DEF%'
ORDER BY date
If you want your names to contain the strings 'ABC' OR 'DEF' anywhere inside, your query would be:
SELECT id,name FROM Regist
WHERE name LIKE '%ABC%' OR name LIKE '%DEF%'
ORDER BY date
Finally, if you want your names to contain both 'ABC' AND 'DEF' you could use:
SELECT id,name FROM Regist
WHERE name LIKE '%ABC%DEF%' OR name LIKE '%DEF%ABC%'
ORDER BY date
Probably you want something like this:
SELECT id,name FROM Regist WHERE NAME LIKE '%ABC%' OR NAME LIKE '%DEF%';
Please note that this kind of search is inefficient and if you need something like that you'd better use something like Sphinx
From the little i understand your question
"SELECT ID,Name FROM Regist where name='ABC' ORDER BY date";
is this correct?
"SELECT id,name FROM Regist WHERE id LIKE '%' OR NAME LIKE '%' ORDER BY date";