Normalizing date in MySQL where one column contains comma delimited items - mysql

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;

Related

MySQL update order (row number) from comma delimited list - the simplest way

I have a table with two columns ID and ORDER, what I want to achieve is to update ORDER column as list numbers (like 1, 2 ...).
I have input as comma-delimited list of IDs:
21545,13117,21538,940,19658,21547,21532,7404,19663,19666,863,13114,13121,11769,13147,13156,972,13165,13174,13182,853,19671,7429,935,1015,931,986,996,991,953,893,920,899,906,20972,886,873,21574,21548
I need to update ORDER so 21545 = 1, 13117 = 2 and so on.
What is the simplest way to achieve this?
Maybe this is an easy task for you, but I am an MSSQL developer, so please don't mind me asking this. Thanks.
You can use the FIELD function to return the position of a value in a set of values if you are building the query from scratch e.g.
SELECT FIELD(13117, 21545,13117,21538,940,19658)
If you have a string which is comma separated, you can use FIND_IN_SET:
SELECT FIND_IN_SET(13117, '21545,13117,21538,940,19658')
In both cases the output is 2.
You can find a demo showing the use of these functions in an UPDATE query on dbfiddle

Searching for entry with id in comma separated list in mysql

I want to get entries from a mysql table, which contain a given id within a comma separated list. I want to use regular expressions and the LIKE selector.
My current approach looks like this
SELECT * FROM table WHERE list LIKE '%,0,%';
with the problem being that this ignores the first and last element in a list like '0,1,2,3'.
I've tried using the | or operator to test for all possible cases.
SELECT * FROM table WHERE list LIKE '(%,0,%)|(^0,%)';
I've tried this with and without the ^ character and with and without the parenthesis, but in all cases this approach didn't even match the characters in the middle. In fact, the or operator doesn't seem to be working in even the simplest expressions like
SELECT * FROM table WHERE list LIKE '%(1|2)%';
You should fix your data model! DO not store lists of things -- especially numbers -- in a string. SQL has a great data model for storing lists: it is called a table.
If you are stuck with someone else's really, really, really bad choice of dta model, you can work around in. MySQL has a handy function, find_in_set(), that does what you want:
WHERE find_in_set('0', list) > 0
Concatenate commas to the start and the end of the list:
SELECT * FROM table WHERE concat(',', list, ',') LIKE '%,0,%';

SELECT odd syntaxes in database

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.

mysql SELECT WHERE string contains characters

I have a table field events which can contains:
Only a sequence of number, for example: 45
or a sequence of number divided by the symbol |, for example 55|65|76
I think i have to use LIKE, but i don't know how. Can you help me?
Thanks
I would recommend using CONCAT to add a pipe | before and after your field, and then using a LIKE search. Something like this:
SELECT *
FROM YourTable
WHERE concat('|',field,'|') like '%|45|%'
SQL FIddle Demo
However, I highly recommend trying to normalize your data. Considering storing these in separate rows which would make searching/maintaining much easier.
To fix your query, use:
Select * from tbl where events='45' or events like '%|45' or events like '%|45|%' or
events like '45|%'
but this is terribly slow and should not be used.
Instead, do as Marc B states and create a child table events (ID, event).

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