mysql query restricting on multiple periods - mysql

I'm trying to write a mysql query to detect three or more periods in a email string. Example
gr.em.test.t#domain.com
I'm thinking I could use some sort of mysql like query, but I'm not sure how to best write the regex. Any thoughts?
SELECT MyColumn
From MyTable
WHERE MyColumn like

No need for any type of regular expression, simply use the LIKE clause like you have, so you're on the right track. The following query should give you what you want:
SELECT MyColumn FROM MyTable WHERE MyColumn LIKE "%.%.%.%";

Related

Query several 'starts with' values in mySQL

I was trying to query values in a table where they started with 'a', 'b' or 'c'. I know in MS SQL you can make a [charlist] to do this:
( LIKE '[abc]%' )
I was wondering what the correct syntax was in other databases such as Oracle or mySQL.
Thanks
In MySQL you can use Regular Expressions
where some_column REGEXP '[abc].*'
MySQL follows the same syntax. Use % to wildcard, or ? single characters:
SELECT * FROM `table` WHERE `column` LIKE 'a%' OR `column` LIKE 'b%'...
Alternatively, You can use REGEXP in MySQL, but it will cause performance hits as you are unable to use indexes in order to query data.
For the SELECT LIKE 'a%' query it will be able to use the index to lookup all items starting with A, and return them efficiently. Using REGEXP will cause a row scan for each item, and performance will be greatly impacted.

MySQL LIke statement - multiple words

What would be the right SQL statement so that when I search two words, like for example 'text field' in a text box, it will return all results that has 'text' and 'field' in it using the LIKE statement? I cant find the right terms to make a search. If possible, I want to make it dynamic. Like if a user search 5 words, all 5 words would be in the Like statement. I am trying to achieve a statement something like this.
SELECT *
FROM TABLE
WHERE SEARCH (LIKE %searchterm1%)
OR (LIKE %searchterm2%)
OR (LIKE %searchterm3%) ....
Try This. http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
SELECT *
FROM TABLE
WHERE SEARCH
REGEXP 'searchterm1|searchterm2|searchterm3'
Here's an example of a SQL SELECT statement that uses the LIKE comparison operator
SELECT t.*
FROM mytable t
WHERE t.col LIKE CONCAT('%','cdef','%')
AND t.col LIKE CONCAT('%','hijk','%')
AND t.col LIKE CONCAT('%','mnop','%')
Only rows that have a value in the col column that contains all of the strings 'cdef', 'hijk', and 'mnop' will be returned.
You specifically asked about the LIKE comparison operator. There's also a REGEXP operator that matches regular expressions. And the Full-Text search feature may be a good fit your use case.

Count(1) and the Like statement Best use/Performance

Is there a better way of doing the below sql query I am not sure if the Like statement is the best option as the location column only contains exact matches.
INSERT INTO test_reports (Table_Name, Total_Count)
SELECT "table1", COUNT(1)
FROM table1
WHERE location LIKE 'birmingham'
There's no wildcards in your search string. Without wildcards, like is exactly the same as =.

MySQL multiple IN clause does not work

I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,

Regex for MySQL query

I am trying to select a field based on it meeting one of 3 criteria... and I'm not sure how to do this. I think a RegExp is probably the best method buy I'm unfamiliar with writing them.
Say I have the integer 123, I would like to match the following cases:
123 (thats 123 only with no spaces or other numbers after it)
123-10/12/2007 00:00 (thats 123 with a hyphen and a date, or actually it could be anything after the hyphen)
123_1014859 (thats 123 with an underscore, or again anything after the underscore)
Is there a way to do this using MySQL?
A regex is plausible, but it's not the best performing option. The last comparison put MySQL's regex support as being par with wildcarding the left side of a LIKE statement -- works, but the slowest of every option available.
Based on your example, you could use:
SELECT t.*
FROM YOUR_TABLE t
WHERE t.column LIKE '123-%'
OR t.column LIKE '123_%'
Another alternative, because OR can be a performance issue too, would be to use a UNION:
SELECT a.*
FROM YOUR_TABLE a
WHERE a.column LIKE '123-%'
UNION ALL
SELECT b.*
FROM YOUR_TABLE b
WHERE b.column LIKE '123_%'
UNION ALL will return all results from both tables; UNION removes duplicates, and is slower than UNION ALL for that fact.
select * from foo where bar regexp '^123-|_'
(not tested)
I would avoid using regex inside a SQL statement. Someone can correct me if I am wrong, but MySQL has to use another engine to run the regex.
SELECT * FROM table
WHERE field like "123"
OR field LIKE "123-%"
OR field like "123_%";