I have a mysql database with a table that contains an email address entered by website users.
How would I select all records where the email field contains any number of characters, then 3 numbers and #yahoo.com
i.e. testemail639#yahoo.com
A simple way would be to use REGEXP in your SELECT statements.
SELECT * FROM records WHERE email REGEXP '^\w+\d{3}\#.*$'
The above statement is untested, but should lead you down a better road.
SELECT * FROM table WHERE email REGEXP '[0-9]{3}#yahoo\.com'
Try this
SELECT * FROM records WHERE email REGEXP '^\w+([\.-]?\w+)?([0-9]{3})#\w+([\.-]?\w+)*(\.\w{2,4})+$'
^\w+([\.-]?\w+)?([0-9]{3})#\w+([\.-]?\w+)*(\.\w{2,4})+$ strictly checks string followed by 3 numbers pattern in the email containing no spaces
Related
I have a field called EMAIL_ADDRESS. One of the records would be:
john#gmail.com, mike#gmail.com, joe#yahoo.com, george#yahoo.com, fred#gmail.com
I wan to remove all yahoo addresses in my SELECT query to get:
john#gmail.com, mike#gmail.com, fred#gmail.com
If I use
REPLACE(SM.SCORECARD_EMAIL_ADDRESS, 'joe#yahoo.com,', '')
this works.
If I want to remove ALL yahoo email addresses this doesn't work:
REPLACE(SM.SCORECARD_EMAIL_ADDRESS, '%#yahoo.com,', '')
because wildcards don't seem to work as it's looking for % in the string.
You should probably fix your table design and stop storing CSV lists of email addresses. Instead, get each email onto a separate record. As a short term fix, if you're running MySQL 8+, you may use REGEXP_REPLACE():
UPDATE yourTable
SET EMAIL_ADDRESS = REGEXP_REPLACE(
REGEXP_REPLACE(EMAIL_ADDRESS, '(, )?\\S+#yahoo\\.com,?', ','), '^,+|,+$', '')
WHERE EMAIL_ADDRESS LIKE '%#yahoo.com%';
If you don't need to udpate records but you want them only in the SELECT query you can use NOT LIKE operator
SELECT * FROM your_table WHERE email NOT LIKE '%yahoo.com'
So you get records that doesn’t match the like pattern
Need help in executing a query over a customer database on a field named phone.
select * from customers where phone REGEXP '123456|565834'
I need a way to select the matched portion of regex matched in select clause.
The final results should be something like
Name Matched Phone
Naveen 123456 12345678
Naveen2 123456 123456789
Arun 565834 9565834
Arun2 565834 10565834
P.S. This has to be one query and there is no other unique key to be grouped by with
Use INSTR function of MySQL.
Ex. INSTR(regex, phone)
SELECT SUBSTRING('123456|565834',INSTR('123456|565834',phone),10)
FROM customers
WHERE phone REGEXP '123456|565834';
Doc: INSTR function
You may use a CASE expression here:
SELECT
Name,
CASE WHEN Phone REGEXP '123456' THEN '123456'
WHEN Phone REGEXP '565834' THEN '565834' END AS Matched,
Phone
FROM customers
WHERE
phone REGEXP '123456|565834';
Note that you don't necessarily need to use REGEXP in this case, LIKE probably would have sufficed. But using REGEXP is easier to read IMO, and also it lets you handle more complex logic, should you need to in the future.
If you're not constrained to only use mysql, then may try with shell script + mysql to pass the map values and search - Not sure understood the requirement correctly.
search="5672C";
mysql <connection details> -e "select fields, \"$search\" as matched from customers where phone like \"%$search%\";"
We can loop the map strings and achieve.
I have a table containing addresses. I would like to perform a query to select rows where the numeric values match.
address1 postcode
13 Some Road SW1 1AA
House 5 G3 7L
e.g
select * from addresses where numeric(address1)=13 and numeric(postcode)=11
^^ That would match the first row
select * from addresses where numeric(address1)=5 and numeric(postcode)=37
^^ That would match the second row
Is this possible?
Yes, this is possible. You could write a function that uses a regular expression to replace all non-numeric characters in the field with the empty character so the result would be only numeric characters returned from that function, and then filter on that function.
You might be interested in this stackoverflow question and this blog post.
See also mysql-udf-regexp.
select * from addresses where address1 REGEXP '(13)' and postcode REGEXP '(11)';
I have a table.
USER TABLE Columns:
user_name (varchar)
age (int)
address (varchar)
telephone (varchar)
I want to seach for user whose address field contains digits.
I tried the following sql statement in MySQL Workbench:
SELECT * FROM user WHERE address LIKE '%[^0-9]%';
However, it did not work. Can anyone help me out here?
Thanks in advance.
You should remove the ^ character from the brackets:
LIKE '%[0-9]%'
It doesn't want to be a regex, it is the [charlist] wildcard
I believe you're looking for REGEXP:
SELECT *
FROM user
WHERE address REGEXP '.*[^0-9].*'
Example of finding addresses without numbers
select * from user where first_name REGEXP '^[0-9].*'
This gives you all records in your user table where the first name starts with numbers 0-9.
'%[^0-9]%'
Will search for the column that has %[^0-9]% as text
It won't do a Regex on it
Try
'.[^0-9].' // That performs regex
I am trying to find records that has the following scenario.
ID | name | email
1 Robert robert#gmail.com
2 William bill#gmail.com
3 Michael michael#gmail.com
4 Micahel mike#gmail.com
Based on the above table, I want to find the records where the "name" is contained in the "email field", here record 1 and 3 should be the output and not 2 and 4. Is there any way I can do this comparison?
I tried reading about regex but couldn't find anything. If it's comparison of same value, it will be straightforward, but I am not having any clue for this one. I thought of LIKE but looks like this cannot have field names.
The exact syntax will depend on how you want to define the relationship.
Are you looking for the name anywhere in the email address? (This will be slow)
select id,name,email
from your_table
where email like concat('%',name,'%')
Just at the beginning of the email address?
select id,name,email
from your_table
where email like concat(name,'%')
Just before the # sign?
select id,name,email
from your_table
where email like concat(name,'#%')
You can use LIKE, you just have to use it in combination with CONCAT.
SELECT
ID,
name,
email
FROM
yourTable
WHERE
email LIKE CONCAT(name, '%');
The CONCAT will return a string which can be used to match against email via LIKE.
This should work
SELECT * FROM table WHERE email LIKE (CONCAT('%',name,'%'))
select * from your_table where lower(substring_index(email,'#',1))=lower(name)