SELECT odd syntaxes in database - mysql

Basically I have a large MySQL database table with a lot of city names, 90% of them are valid entries, but some of them are written in a ... not valid way.
For example the valid way is juste "CITYNAME" but some of them are like "(CITY NAME)(COUNTRY)" or just "(CITY NAME)" so I just wanna SELECT all the entries that are not written the valid way.
I don't know if that's specific enough don't hesitate to ask me for some precise elements.
And please help I have no idea how to build my SQL query.
CREATE TABLE cities(
name VARCHAR(50) NOT NULL PRIMARY KEY
);
INSERT INTO cities(name) VALUES ('ORLANDO');
INSERT INTO cities(name) VALUES ('(CHARLOTTE)');
INSERT INTO cities(name) VALUES ('(PHOENIX)(USA)');
INSERT INTO cities(name) VALUES ('AUSTIN(USA)');
INSERT INTO cities(name) VALUES ('TENNESSEE NASHVILLE');
So here are some examples of the different kinds of entries that I have to deal with.
I don't know how to describe the desired output, that'd just be a list of odd syntaxes, with or without the brackets.
The whole point is to delete those odd entries, but I have to SELECT them before doing so. And I also won't be the one deleting them, just gotta SELECT.

Possible solutions with output:
select * from cities where instr(name,')') or instr(name,'(');
(CHARLOTTE)
(PHOENIX)(USA)
This looks for anything which contains "(" or ")" anywhere.

Why wouldn't you try something like
Select * from cities where name like '(%';
If the problem is the parenthesis.. then everything that starts with a parenthesis will be selected
EDIT:
Select * from cities where name like '%(%' or name like '%)%';
It gives every line that contains a ( OR a )
EDIT2: some explanation
The character '%' replaces any string. So if you put like '%randomthing%', it will look for everything that look like anystring_randomthing_anystring. I hope i made this clear

You are using MySql, so you can do:
select *
from cities
where name not regexp '^[A-Z-]+$';
The not regexp '^[A-Z-]+$' means all the city names that don't respect the format "AAAA" or "AAAA-AAAAA" (where A represent an uppercase letter) is considered as invalid.

Related

mysql MATCH AGAINST weird characters query

I have a table where the field "company_name" has weird characters, like "à","ö","¬","©","¬","†", etc. I want to return all "company_name"s that contain these characters anywhere within the string. My current query looks like this:
SELECT * FROM table WHERE
MATCH (company_name) AGAINST ('"Ä","à","ö","¬","©","¬","†"' in natural language mode);
But I keep getting no data from the query. I know this can't be the case, as there are definitely examples of them I can find manually. To be clear, the query itself isn't throwing any errors, just not returning any data.
The minimun word length is 3 pr 4 .
you can change it see manial
https://dev.mysql.com/doc/refman/8.0/en/fulltext-fine-tuning.html
or use regular expressiions
SELECT * FROM table WHERE
ompany_name REGEXP '[Äàö¬©¬†]+';
SELECT *
FROM table
WHERE company_name LIKE '%[^0-9a-zA-Z !"#$%&''()*+,\-./:;<=>?#\[\^_`{|}~\]\\]%' ESCAPE '\'
This will find any wacky stuff, including wide characters or 'euro-ASCII' or emoji.

Search comma separated string using LIKE in mysql

I have a table with 3 columns something like below,
expert table
id - 1589
name - Jhonny
expert_in - 1,12,8 (Values similar like this)
The experts_in contains another table's foreign key
experts_in table
id - 1
expert_in - painting
I want search experts who are expert in some jobs while searching for experts
SELECT * FROM `experts` WHERE expert_in LIKE 1%
The above query brings all experts with 11,12,13...etc. I want only exact word. I know LIKE will bring all. Is there any way to achieve this without altering table. Thanks in advance.
You should use REGEXP. Try this query:
SELECT * FROM experts
WHERE expert_in REGEXP '[[:<:]]1[[:>:]]';
Output: See Live Demo on SQLFiddle
Note: You can adjust searching string based on your requirement above REGEXP is searching exact word.
if you can alter the data (not the table/schema) you should append and prepend another comma, so you can search with where col like "%,123,%", this will always fit on an exact value. Otherwise you have to use regex with something like ,?123,?

how to skip the inverted commas when selecting a column from MySQL

There is a table with fields name, age, city and state. Now I need to select rows based on city name. The value of the column city is surrounded with ", for example "LA".
How can I write a SELECT statement for getting data based on city.
\" is the escape combination for double quotes:
SELECT * FROM mytable WHERE city = '\"LA\"';
See MySQL documentation "String Literals".
Just suggestion, if you are collecting and storing the information in the table to be queried later (ie you are in control of the input), try to clean the data up before storing it to make it easier to query?
If the input has quotes and white space, clean that before inserting the values into the table. Use programming to do this, or mySQL: TRIM() and REPLACE() to remove the characters that might make a query hard to build and then store the resulting value into the table.
Of course, if you do not have control of the input data, that is where the answers above and the challenge to a programmer begins, trying to figure out the different input possibilities and dealing with that.
SELECT *
FROM table1
WHERE city LIKE '%LA%'
or
SELECT *
FROM table1
WHERE city REGEXP '^[[:space:]]*LA[[:space:]]*$'

Normalizing date in MySQL where one column contains comma delimited items

I've got a database with one field that contains a comma delimited list that I'd like to normalize. The field would look something like this...
16-BIT, 20 MHz, MICROPROCESSOR, PQFP100
It will not always have the same number of commas delimited items, however the only item I care about, and the one I want to pull out and normalize is always going to be the last one, in the case above 'PQFP100'.
I think I understand the sql to get the column out and into another table, but I don't know how to select only the PQFP100 part of it. Here's what I have so far...
insert ignore into part_pkg (pkg_name)
select part_desc
from part_raw
group by pkg_name;
I think I need something on the 'from part_raw' part but don't even know where to start :)
Hopefully this is clear enough. Thanks
if you select this:
select substring_index('16-BIT, 20 MHz, MICROPROCESSOR, PQFP100',',',-1)
you will get this:
PQFP100
So the insert statement must be something like this:
insert ignore into part_pkg (pkg_name)
select substring_index(part_desc',',',-1)
from part_raw
group by pkg_name;
Try this:
insert ignore into part_pkg (pkg_name)
select SUBSTRING_INDEX(part_desc, ',', -1)
from part_raw
group by pkg_name;

MySQL UNION query correct handling for 3 or more words

I've to ask your help to solve this problem.
My website has a search field, let's say user writes in "Korg X 50"
In my database in table "products" i have a filed "name" that holds "X50" and a field "brand" that hold "Korg". Is there a way to use the UNION option to get the correct record ?
And if the user enters "Korg X-50" ?
Thank you very much !
Matteo
May be you should use full-text search
SELECT brand, name, MATCH (brand,name) AGAINST ('Korg X 50') AS score
FROM products WHERE MATCH (brand,name) AGAINST ('Korg X 50')
As far as I understand you don't need UNION but something like
SELECT * FROM table1
WHERE CONCAT(field1, field2) LIKE '%your_string%'
On client side you get rid of all characters (like space, hyphen, etc) in your_string that appears in user input and cannot be in field1 or field2.
So, user input Korg X 50 as well as Korg X-50 becomes KorgX50.
you will need to get some form of searchable text.
either parse out the input for multiple key words and match each separately, or perhaps try to append them all together and match to the columns appended in the same way.
you will also need either a regex, or maybe a simpler search and replace to get rid of spaces and dashes after the append before the comparison.
in general, allowing users to search for open ended text strings is more complicated than 'what union do i use'... you will ideally also be worried about slight misspellings and capitalization, and keyword order.
you may consider pulling all keywords out from your normal record into a separate keyword list associated with each product, then use that list to perform your searches.
If you do not want to parse user input and use as it is, then you will need to use a query like this
select * from products where concat_ws(' ',brand,name) = user_input -- or
select * from products where concat_ws(' ',brand,name) like %user_input%
However, this query won't return result if user enters name "Korg X-50" and your table contains "Korg" and "X50", then you need to do some other thing to achive this. You may look at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex however it won't be a complete solution. Look for text indexing libraries for that ex: lucene