Replacing strings in MySQL? - mysql

I have those strings in y table:
abcdefg_1056-DF or
123erttzz-1292 or
gdfgdfg_1056
What I want is only the first part, abcdefg for example for the first string. So I can replace all numbers and all -DF with an empty string, but I dont know how.
Ideas?

I would suggest a regex replace. Have a look here - I think this might help you:
How to do a regular expression replace in MySQL?

If you prever a lazy and ugly method (and not recommended for very, very much rows), you can spare yourself a user defined function like in mwerner's answer and do it simply like this:
select
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(asdf, '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', ''), '_', ''), '-DF', '')
from
(
select
'abcdefg_1056-DF' as asdf
union select
'123erttzz-1292'
union select
'gdfgdfg_1056'
)q

Related

Finding all possible complete pairings from a list in Google Sheets

I've got a single list of names, and I'd like to generate every possible combination, where every person on the list is paired uniquely, in a non-repeating way, using Google Sheets. The list of names is variable, and may occasionally be an odd number.
So for example if I had four people, the list would look like this, only the numbers would be replaced with actual names.
(1,2)(3,4)
(1,3)(2,4)
(1,4)(2,3)
Once I have the complete list of combinations, I can sort it myself to find which ones have been least used before.
Try python itertools. https://docs.python.org/2/library/itertools.html
import itertools
nameList = ['john', 'joe', 'jimmy', 'jack']
for name1, name2 in itertools.combinations( nameList, 2 ):
print( '({}, {})'.format( name1, name2 ))
If you want both (name1, name2) and (name2, name1) try itertools.permuations
=QUERY(ARRAYFORMULA(UNIQUE(
TRANSPOSE(SPLIT(REPT(CONCATENATE(A1:A4&CHAR(9)), COUNTA(A1:A4)), CHAR(9)))&" "&
TRANSPOSE(SPLIT(CONCATENATE(REPT(A1:A4&CHAR(9), COUNTA(A1:A4))), CHAR(9))))),
"select Col1 where Col1 is not null", 0)
=TRANSPOSE(QUERY(TRANSPOSE(QUERY(ARRAYFORMULA(UNIQUE(
TRANSPOSE(SPLIT(REPT(CONCATENATE(A1:A4&CHAR(9)), COUNTA(A1:A4)), CHAR(9)))&" "&
TRANSPOSE(SPLIT(CONCATENATE(REPT(A1:A4&CHAR(9), COUNTA(A1:A4))), CHAR(9))))),
"select Col1 where Col1 is not null", 0)),
"select Col2,Col3,Col4,Col7,Col8,Col12", 0))
Thank all for your help. I think my query was lacking in detail. I was hoping for a table result, where each line was a thorough result of every person/string being paired with another string individually.
I'm bad at coding, so I was inspired to do it in python, using a recursive function and a little math wizardry. I was able to make it accept a variable list and expand accordingly.
nameList = ['1', '2', '3', '4', '5']
if(len(nameList) % 2 == 1):
nameList.append(" ")
print(nameList)
length = len(nameList)
possible = 1
while length > 1:
possible = possible*(length-1)
length -= 2
def completeCombinations (List, line):
BaseList = list(List)
if (line % (len(List)-1) == 0):
num = (len(List)-1)
else:
num = line % (len(List)-1)
BaseList.remove(List[0])
BaseList.remove(List[num])
if(len(List) > 2):
CurrentList = list(completeCombinations(BaseList, line))
CurrentList.insert(0, List[num])
CurrentList.insert(0, List[0])
else:
CurrentList = list(List)
return CurrentList
i = 1
while i < possible:
print completeCombinations(nameList, i+1)
i += 1
And the results look pretty close what I'd hoped, if not necessarily pretty.
['1', '2', '3', '4', '5', ' ']
['1', '3', '2', '5', '4', ' ']
['1', '4', '2', ' ', '3', '5']
['1', '5', '2', '3', '4', ' ']
['1', ' ', '2', '4', '3', '5']
['1', '2', '3', ' ', '4', '5']
['1', '3', '2', '4', '5', ' ']
['1', '4', '2', '5', '3', ' ']
['1', '5', '2', ' ', '3', '4']
['1', ' ', '2', '3', '4', '5']
['1', '2', '3', '5', '4', ' ']
['1', '3', '2', ' ', '4', '5']
['1', '4', '2', '3', '5', ' ']
['1', '5', '2', '4', '3', ' ']
['1', ' ', '2', '5', '3', '4']
Heartfelt thanks to anyone who can help me pretty it up and import the results into google sheets

MySQL - return values in order provided

In MySQL, I run a lot of SELECT statements with IN clauses. Sometimes I want to see the output table ordered by the order of values that I used in the IN clause, rather than alpha/numerical.
If I ran
SELECT id FROM x
WHERE id IN ('1', '3', '2', '0')
I would want output 1, 3, 2, 0, not 0, 1, 2, 3.
Is there a way to effect this?
Thanks!
With the following query you should get what you want:
SELECT id FROM x WHERE id IN ('1', '3', '2', '0') ORDER BY FIELD(id, '1', '3', '2', '0')
From the MySQL specification about FIELD():
Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_field

Is this query sufficient to checksum a database?

I'm using Elasticbeanstalk (and a MySQL RDS) for my client application and an EC2 to act as a utility server to keep all individual client databases up to date. Because Elasticbeanstalk is dodgy when it comes to files unless I'm using an S3 I figured it'd be better to use a database only solution. My goal is to create a checksum of each client database and track a running history of expected checksums to know when it's time to update.
My question is: What is the best way to easily determine that a production schema matches the installed schema in MySQL?
I wrote this query to handle it, but are there any pitfalls doing it this way?
SELECT MD5(GROUP_CONCAT(
COALESCE(TABLE_NAME, ''),
COALESCE(COLUMN_NAME, ''),
COALESCE(ORDINAL_POSITION, ''),
COALESCE(COLUMN_DEFAULT, ''),
COALESCE(IS_NULLABLE, ''),
COALESCE(DATA_TYPE, ''),
COALESCE(CHARACTER_MAXIMUM_LENGTH, ''),
COALESCE(CHARACTER_SET_NAME, ''),
COALESCE(COLLATION_NAME, ''),
COALESCE(COLUMN_TYPE, ''),
COALESCE(COLUMN_KEY, ''))),
SHA1(GROUP_CONCAT(
COALESCE(TABLE_NAME, ''),
COALESCE(COLUMN_NAME, ''),
COALESCE(ORDINAL_POSITION, ''),
COALESCE(COLUMN_DEFAULT, ''),
COALESCE(IS_NULLABLE, ''),
COALESCE(DATA_TYPE, ''),
COALESCE(CHARACTER_MAXIMUM_LENGTH, ''),
COALESCE(CHARACTER_SET_NAME, ''),
COALESCE(COLLATION_NAME, ''),
COALESCE(COLUMN_TYPE, ''),
COALESCE(COLUMN_KEY, '')))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE();

How to do TAB spacing while inserting values in a table in phpmyadmin?

I want to tab space my data while inserting through sql in phpmyADMIN.
What should I do?
INSERT INTO `questions` (`id`, `question_name`, `answer1`, `answer2`, `answer3`, `answer4`, `answer5`, `answer6`, `answer`, `category_id`) VALUES(90,'C<br> java', '1', '2', '3', '4', '', '', '3', 2);
You can split the string up and place a tab in between: 'some' + CHAR(9) + 'string'. Note that 9 is the ASCII code for a horizontal tab. Perhaps easier is 'some\tstring', which is syntax common to many other languages.
See table 9.1 here: http://dev.mysql.com/doc/refman/5.0/en/string-literals.html

Insert row for each userid for fields from other table

I am learning MySQL and so don't know complex query. So need your expert help.
I have rc_userfields table with 4 fields, key and position which I already have. Now I want to add this 4 fields into rc_profile table. I want to add it for all userid. Means all 4 fields will be added for all userid.
rc_userfields table
Userid can be refereed from rc_users table
rc_users table
So final result would be something like below image
rc_profile table
Please refer below image for all three tables:
I know how can I insert manually with below code:
INSERT INTO `rc_profile` (`userid`, `keys`, `data`) VALUES
(1, 'myself', ''),
(1, 'position', ''),
(1, 'nickname', ''),
(1, 'blog', ''),
(2, 'myself', ''),
(2, 'position', ''),
(2, 'nickname', ''),
(2, 'blog', ''),
(3, 'myself', ''),
(3, 'position', ''),
(3, 'nickname', ''),
(3, 'blog', ''),
(4, 'myself', ''),
(4, 'position', ''),
(4, 'nickname', ''),
(4, 'blog', '');
Finally: I want to add all 4 fields keys from rc_userfields table to rc_profile table for every user in rc_users table
You need get the cartesian product of the two tables using CROSS JOIN and the result will be inserted on table rc_profile, eg.
INSERT INTO rc_profile (userID, `keys`, data)
SELECT a.userID, b.key, '' data
FROM rc_users a CROSS JOIN rc_userfields b
SQLFiddle Demo