MySQL query to perform pattern matching - mysql

In MySQL how to search a value in a field that contains value separated by commas?(I have to search for (5,6) and it should return the field that contains all possibilities {5,6,(5,6)}
Is it possible?

You can try this
MyModel.where("field LIKE (?)", "%5,6%")

MyModel.where("filed REGEXP '5,6'")

There are many methods in MySQL for pattern matching, such as like ,RLIKE, MATCH-AGAINST,FIND_IN_SET, etc. The exact one depends on your need. Explain your sample data and desired result; then I can say which function will best help you.

Related

MySql extract data from a database based on two columns

I Am trying to create/run an my sql query in such a way that the sql selects data based a some conditions from Column 1 (USER) but at the same time Excludes some data, based on some conditions from column 2 (ADDRESS)
E.g.:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE FIELD(USER,'%AMIT%','%JOHN%','%SANDEEP%','%WARNE%')
AND ORIGINATING_ADDRESS NOT LIKE 'MUMBAI','CHINA','PAKISTAN'
This is giving error.Can some one please help ?
Use NOT IN to discard list of values from select. Considering that you want to discard when there is exact match
ORIGINATING_ADDRESS NOT IN ('MUMBAI','CHINA','PAKISTAN')
When you want to use pattern search and discard the use this
ORIGINATING_ADDRESS NOT LIKE '%MUMBAI%' OR
ORIGINATING_ADDRESS NOT LIKE '%CHINA%' OR
ORIGINATING_ADDRESS NOT LIKE '%PAKISTAN%'
For a set of values, use NOT IN, instead of NOT LIKE.
You might find regular expressions simpler for this purpose:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE USER REGEXP 'AMIT|JOHN|SANDEEP|WARNE' AND
ORIGINATING_ADDRESS NOT REGEXP 'MUMBAI|CHINA|PAKISTAN';

What should be the Table structure for following situation

I have a situation where i need to store "Error Type" which has following options
I want to know what is the best way I should use to create my table "Error".
So either I take a "VARCHAR" data type and store values like "1,3,4" (Comma Separated) if "Take Off, Details and Legend" is selected and parse it when getting in view
OR
Take separate column for each field in table with data type "TINYINT" like "IsTakeOff" , "IsSpecifications" ,"Details" etc.
Please advice
Thanks
If user with id 1 select 1,3 and 4 then you can use following
Don't use multiple columns unless you are very confident the number won't grow.
Otherwise use a many-to-many association table - one with columns for the id of the item and the id of the error
One way to go might be to do it as an int and then in your code do something like this PHP, I don't know language you are using but most languages have a switch so it shouldn't be to hard to translate to another language.
$row // The row from the database.
switch ($row['Error_Type'])
{
case ('0'):
{
// Do something.
break;
}
...
}
You can use comma seperated column here that is varchar because you know that no of records will not grow here. And this is limited to only 5 values. And while searching the field you can use MySQL FIND_IN_SET which is very effective for this kind of situations. FIND_IN_SET will take two parameters. Your search keyword and the comma seperated string.

MySQL query for "starts with but may not fully contain"

Is there a way to do a MySQL query for data fields that start with but may not fully contain a given string?
For instance, if I had the following list of data items:
my_table
1. example.com
2. example.com/subpage
3. subdomain.example.com
4. ain.example.com
5. ple.com
I would like to feed
"example.com/subpage" and return #1, #2
"example.com" and return #1
"wexample.com" and return nothing
"exa" and return nothing
"subdomain.example.com/subpage" and return #3
Thanks a lot!
Given:
CREATE TABLE paths ( path VARCHAR(255) NOT NULL );
Searching for "example.com/subpage" would require the following query:
SELECT * FROM paths WHERE INSTR("example.com/subpage", path) = 1;
Just don't try to run it over a large dataset frequently...
Docs: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr
Since your test data indicates you don't want character-by-character matching (but something more like component by component), split the input into the components and search on all prefixes.
If you want to return results for example.com but not exam, you are NOT searching for something that "starts with" yuour input. Not sure if the question is wrong or the examples there.
If the examples are correct, you're going to need to do something to identify if your input is a URL or not using pattern matching like regex or at least specify some solid rules around what you want to match. You'll probably need to explain those rules before a correct recommendation can be made too.
It might be as simple as extracting anything before the "/" if there is one or using your application to break up your request to a url component and a path component.
Mode info on regex in mysql
It seems that you want the column value to match the start of your pattern:
SELECT * FROM my_table WHERE 'example.com' LIKE CONCAT(my_table.my_column, '%');
The downside of this is that it isn't going to use any indexes on my_column.

Extracting information out of a column with regexs in MySQL

Is it possible to get the (first?) match of a regex and output it within a select? It looks like the REGEXP function only return whether there has been a match or not. I want to be able to extract information out of a varchar column without having to use complex SUBSTRING-LOCATION nestings.
Any ideas?
http://dev.mysql.com/doc/refman/5.1/en/regexp.html that's all there is. You can't do more than pattern comparison.

Search prob in mysql query

Is there any way to search like below criteria in mysql?
If any the pl reply.
if i search with "murray's" then it then it will return fine data for "murray's" but it should also return data for "murrays" means same word without apostrophy(').
same way if i search without apostrophy('), the it will search also with apostrophy(').
at last
if search query is "murray's", the it will return "murray's" and "murrays" also.
and
if search query is "murrays", the it will return "murrays" and "murray's" also.
Thanks in advance
Your best bet is to create a Full Text Index on your table.
You can then query it as:
select *
from restaurants
where match(name) against ('murrays')
Barring that, you can use SOUNDEX or SOUNDS LIKE in your query. The following two queries are exactly identical:
select *
from restaurants
where soundex(name) = soundex('murrays')
select *
from restaurants
where name sounds like 'murrays'
Also note that MySQL uses the original SOUNDEX algorithm, not the more recent one. Therefore, it will return arbitrary length strings. If you've used SOUNDEX before, just make sure you take that into account.