Get list of address dont have UK address mysql - mysql

I have a MySQL table contains a list of UK address, I was trying to get the list of address doesn't have a postcode.
I the list, we can see some of them don't have postcode at the end.
I was written a query as follows and didn't get the expected result.
select * from property_address WHERE property_address
REGEXP '^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$'
How to fix this query get working?

I will assume the postcode regexp is correct.
REGEXP '^([A-PR-UWYZ0-9]...|GIR 0AA)$'
______ _
You need to remove the "anchor" (^) that I underlined above. It is not "not". Instead, negate thus:
NOT REGEXP '([A-PR-UWYZ0-9]...|GIR 0AA)$'
___ _
Akina's suggestion of first extracting via SUBSTRING_INDEX is likely to make the query faster.

Related

Mysql regex search over an array of phone numbers

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.

MySQL wildcard query

I'm relatively new with SQL and I ran into a problem/question while doing some practice problems.
https://www.hackerrank.com/challenges/weather-observation-station-8
Here's the query I used with MySQL:
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE '[aeiou]%[aeiou]'
I'm confused why this doesn't work. Here's my thinking for the query:
SELECT DISTINCT CITY
^ make sure each city returned isn't repeated
FROM STATION
^ from the STATION table
WHERE CITY LIKE '[aeiou]%[aeiou]'
^ CITY names that are selected can have the first letter begin with [aeiou], have anything in between, and end with [aeiou].
Any help or advice would be appreciated, thanks!
If you are using regex, you can use regexp or RLIKE in place of LIKE. The other thing you need to do is put ^ to denote the first character, $ to denote the last character, and .* for wildcard. See this and this:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY RLIKE '^[aeiou].*[aeiou]$'
The like operator has only 2 wildcard characters: % and _, it does not handle the bracket syntax. You need regular expressions for that, and you can use the rlike operator for that. But in regular expressions you need to use . instead of _ and .* instead of %.

mySQL pattern matching a comma separated list

I have a mySQL table that contains restaurant information. Part of that information is a comma separated list of numbers that corresponds to the type of cuisine the restaurant serves. I'm having some problems getting the correct information out of the database. Table looks like
id businessName cuisine_id
1 Pizza Place 2,3,4,
2 Burger Place 12,13,14,
I came up with
SELECT * FROM restaurant WHERE cuisine_id LIKE "%2,%"
But that leaves me with the problem that it matches "2," "12," and 22,".
I also tried
SELECT * FROM restaurant WHERE cuisine_id LIKE "[^0-9]2,%"
But that returned nothing.
Any advice on how to write this expression?
Use regexp
SELECT * FROM restaurant WHERE cuisine_id REGEXP "(^|,)2,"
For num exact match,
SELECT * FROM restaurant WHERE cuisine_id regexp "(^|,)2(,|$)"
Note that ^, $ are mentioned as regex anchors which matches the start and end end of a line. This (^|,) will match either a start of a line or comma. So this ensured that the following pattern must be at the start or preceeded by comma.
There's no need to use regular expressions, you could use FIND_IN_SET string function:
SELECT * FROM restaurant WHERE FIND_IN_SET('2', cuisine_id)>0
or use CONCAT:
SELECT * FROM restaurant WHERE CONCAT(',', cuisine_id, ',') LIKE '%,2,%'
or better to normalize your database structure (is often not a good idea to store comma separated values in a single field)

How to extract numeric values from text data in MySQL query

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)';

MySQL search match at least one word on query string

i'm making a query in mysql but i have a problem: this is my column structure
|country|
--------
Boston
--------
Chicago
--------
washington
The thing is i may have a search item like:
North Washington
Boston Easht
South Chicago
So i'm trying to match it using the %like% operador like that:
select * from am_ciudad where Ciu_Nombre LIKE '%NORTH BOSTON';
select * from am_ciudad where Ciu_Nombre LIKE 'CHICAGO%';
select * from am_ciudad where Ciu_Nombre LIKE '%South Chicago%';
the second one makes match because it starts with "chicago" word, but in the case of the query has a prefix it doesn't, is there a way to search by at least one match in the query string?
IN method
Use comma separated list of your search query:
SELECT * FROM am_ciudad WHERE Ciu_Nombre IN('North', 'Washington', ...)
REGEXP method
I can imagine the REGEXP will be slower, but I haven't benchmarked it.
SELECT * FROM am_ciudad WHERE Ciu_Nombre REGEXP(North|Washington|...)
Your other searches won't match because they do not exist.
If you want to match Boston in the phrase I love Boston Red Sox then you would need to use ...LIKE '%Boston%'; the %s are wild cards so using them before and after the word you are tying to match tell the query that you don't care what come before and after. Your search string of ...LIKE '%NORTH BOSTON'; is telling query that you are looking for <anything>North BOSTON; which obviously you don't have.
Hopefully that makes sense and helps out.
S
Im not sure if your version of mysql supports it but its worth trying.
select * from am_ciudad where Ciu_Nombre in (NORTH,BOSTON);
Same for the others, just replace the space with ','