Search 2 columns for parts of string in MYSQL - mysql

I would like to search inside 2 columns for parts of a string. It's a backwards way of searching so I doubt it's possible.
I think part of it would have something to do with:
REPLACE( event_name, ' ', '')
and
REPLACE( venue_name, ' ', '')
to get rid of the space.
I also thought REGEX might come into it.
Absolutely no idea where to start! PSUEDOCODE might be:
CONCAT(
event_name = REPLACE( part of :search, ' ', '')
,
venue_name = REPLACE( part of :search, ' ', '')
)
= :search
If I used noddybobstheatre as the search term I want to search the column event_name for part of it and venue_name for another part and when the 2 parts are put together they equal the search term.
event_name = 'noddy'
venue_name = 'bobs theatre'
Like I said, this might be a crazy ask...

Reading what you need I thought that the like statement should use the column and not the search value.
here is my reproduction of what I understood from your problem
CREATE TABLE movies
(`word1` varchar(5), `word2` varchar(7))
;
INSERT INTO movies
(`word1`, `word2`)
VALUES
('micko', 'to'),
('he', 'llbrane'),
('mick', 'oto'),
('hell', 'brane')
;
then here is the sql statement that I used
select * from movies where 'mickoto' like CONCAT(Replace(word1,' ',''),'%') and 'mickoto' like CONCAT('%',Replace(word2,' ',''))
all you have is to adapt this to your context
here is the fiddle execution

use this,
concat(replace(event_name,' ',''),replace(venue_name,' ',''), )=:search

Related

Query with ommited period

I have a table, where one of the columns contains city name.
Most cities are OK, but some may have abbreviations with or without a period
Ft. Worth
Ft Worth
If I have a value to Without a period Ft Worth how can I query the table to find all matching cities?
Use a regular expression to make the period optional:
WHERE city RLIKE 'Ft[.]? Worth'
If you want to ignore all periods in the table when comparing with a search string:
WHERE REPLACE(city, '.', '') = 'Ft Worth'
I might be inclined to use replace():
select replace(city, '.', '') = 'Ft Worth'
One nice aspect of this approach is that you can use in:
select replace(city, '.', '') in ('Ft Worth', 'Gun Barrel City', 'Kermit')
However, I would be suspicious and think that there might be other problems. For instance, you might have "Fort Worth".
In any case, you should clean the data:
update t
set city = 'Ft Worth'
where city = 'Ft. Worth';
or
update t
set city = replace(city, '.', '')
where city like '%. %';

omit results starting with certain character

I am trying to retrieve a list of names from a table where the surname does not start with z or Z using mysql. I tried combining substring with instr to accomplish this. Attempt below:
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE SUBSTRING(
CONCAT(FName, ' ' ,SName),
INSTR(CONCAT(FName, ' ' ,SName), ' ') +1,
1)
<> 'z'
OR SUBSTRING(
CONCAT(FName, ' ' ,SName),
INSTR(CONCAT(FName, ' ' ,SName), ' ') +1,
1)
<> 'Z'
ORDER BY SName
My attempt is returning results with z as the first letter of the surname. Can anyone explain why? Or if there is a better way to achieve this
This can be much shortened with LIKE:
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE FName NOT LIKE 'z%' AND FName NOT LIKE 'Z%';
IIRC LIKE is case-sensitive since MySQL v5.6.x
I wouldn't write it like
...WHERE LOWER(FName) NOT LIKE 'z%';
since applying functions on columns prevent MySQL from using the index on the column (if one exists).
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE FName REGEXP '^[^z]';

Find as close as exact matches in database - which way is better?

I have a situation:
I have a database (MySQL) which contains products and their codes like this
BLACK SUGAR BS 709
HOT SAUCE AX889/9
TOMY 8861
I got an excel spreadsheet which I converted to CSV, this contains prices for the products. Its structure consists in 2 columns, code and price, like this:
BS709 23.00
AX 889 /9 10.89
8861 1.69
I made a script to update the products prices by searching in the database for the respective product code, using a FOREACH and %LIKE% query.
FOREACH row in CSV, search the database using "WHERE product_code LIKE %code%.
This is offcourse a primitive and not so succesfull way of updating the prices, because the codes in CSV are not an exact match (in syntax) of those in the database so if I have two products in the DB containing BS709 (BS70923) in their code I get multiple matches.
Is there a better way of doing this ?
You could trim the columns of spaces and other characters using MySQL replace() before comparing. This will return all exact matches, regardless of any spaces contained.
SELECT * FROM table WHERE REPLACE( product_code, ' ', '' ) LIKE 'code'
Given your examples, I would recommend removing all spaces from both, and then just looking for when the beginning or end of a code matches exactly:
where replace(e.code, ' ', '') like concat(replace(db.code, ' ', ''), '%') or
replace(e.code, ' ', '') like concat('%', replace(db.code, ' ', '')) or
replace(db.code, ' ', '') like concat(replace(e.code, ' ', ''), '%') or
replace(db.code, ' ', '') like concat('%', replace(e.code, ' ', ''));
This may not work for the specific case when one code is a prefix of another.
In any case, if the product codes in a spreadsheet are different from the product codes in the database, I think you have bigger problems. If you cannot really fix the spreadsheets, I would recommend that you manually/semi-automatically create a synonyms table in the database. This would have the Excel product code in one column and the correct product code in the other. Then you can do the lookup just by joining this together.
Yes. That is work. But probably less work than struggling with this problem and getting poor results that have to be repeatedly updated.

Find duplicate phone numbers (even if formatting differs)

I am using the following query to select duplicate phone numbers from a table.
SELECT id, REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( phone, "+", '' ) , ")", '' ) , "(", '' ) , "-", '' ) , ' ', '' ) AS strippedPhone
FROM `customers`
GROUP BY strippedPhone
HAVING count( strippedPhone ) >1
LIMIT 0 , 300
It looks ugly and does not consider the possibilities of alphanumeric character on the field having the phone number.
Any better ways?
This StackOverflow post MySQL strip non-numeric characters to compare has a solution that uses the NumericOnly function.
http://venerableagents.wordpress.com/2011/01/29/mysql-numeric-functions/
In my opinion you would be to normalize the phone numbers before you stored them in the database. Any sql query that is able to compare phone numbers regardless of format is going to be highly inefficient. Where as if you if you reformat the phone numbers before inserting them into the database checking for duplicate numbers is trivial.
If you want to do this on an existing database you will have to normalize the data in the database first but that only has to be done once.

MySQL replace all whitespaces with -

how could i remove ALL whitespaces from a row?
I see here alot of same question but all answers ar to use replace option. Replace will work only to strip one spaces, not all.
ex: a b c to become a-b-c
Thanks.
This can be achieved with the following MySQL Function:
SELECT REPLACE( table.field, ' ', '-' ) FROM table;
This should replace all the whitespace to a -
update image set path = REPLACE( image.path, ' ', '-' ) where path like '% %'
if you would like to update the path in mysql itself use the update for all rows which have spaces withe %20
Try this
replace('a b c',' ','-')
UPDATE table SET table.field = REPLACE( table.field, ' ', '-' );
This will update all the fields, replacing all spaces with hyphens. This will actually modify the data in the tables. Fokko's answer above will change only the data that is pulled, therefore not changing the actual data.