MySQL Remove characters from column headers - mysql

All my column headers in a MySQL database are prefixed with a number, 1_X, 2_X, etc... which makes bringing the data into IDL impossible using just a basic select statement to bring in the entire table. I'm not sure but I see two possible ways:
1) Bring in the table with column name aliases. Can I use TRIM or SUBSTRING_INDEX to remove/replace the first two characters?
2) Create a routine that uses the information schema to to recursively go through and delete the first two characters of the column headers and create a new table with those headers and copy the data in.
If there weren't so many different tables (all with 1_X, 2_X, etc...) there'd be no problem manually selecting 1_X AS X but that's not feasible. It would be great to be able to use TRIM/SUBSTRING on column headers in the select statement.
Thanks.

It's not possible to use functions in a SQL statement to alter the identifier assigned to a column being returned. The SQL way of specifying the identifier for the column in a resultset is to use the expr AS alias approach.
Rather than trim off the leading digit characters, you could prepend the identifiers with another valid character. (Trimming off leading characters seems like it would potentially lead to another problem, duplicate and/or zero length column names.)
You could just use a SQL statement to generate the SELECT list for you.
(NOTE: the GROUP_CONCAT function is limited by some system/session variables: group_concat_max_len and max_allowed_packet, it's easy enough to adjust these higher, though changing global max_allowed_packet may require MySQL to be restarted.)
To get it back the SELECT list on all one line (assuming you won't overflow the GROUP_CONCAT limits) something like:
SELECT c.table_schema
, c.table_name
, GROUP_CONCAT(
CONCAT('t.`',c.column_name,'` AS `x',c.column_name,'`')
ORDER BY c.ordinal_position
) AS select_list_expr
FROM information_schema.columns c
FROM information_schema.columns c
WHERE c.table_schema = 'mydatabase'
GROUP BY c.table_schema, c.table_name
Or, you could even get back a whole SELECT statement, if you wrapped that GROUP_CONCAT expression (which produces the select list) in another CONCAT
Something like this:
SELECT CONCAT('SELECT '
, GROUP_CONCAT(
<select_list_expr>
)
, ' FROM `',c.table_schema,'`.`',c.table_name,'` t;'
) AS stmt
FROM information_schema.columns c
WHERE c.table_schema = 'mydatabase'
GROUP BY c.table_schema, c.table_name
You could use a more clever expression for <select_list_expr>, to check for leading "digit" characters, and assign an alias to just those columns that need it, and leave the other columns unchanged, though that again introduces the potential for returning duplicate column names.
That is, if you already have columns named '1_X' and 'x1_X' in the same table. But a carefully chosen leading character may avoid that problem...
The <select_list_expr> could be more clever by doing a conditional test for leading digit character, something like this:
SELECT CONCAT('SELECT '
, GROUP_CONCAT(
CASE
WHEN c.column_name REGEXP '^[[:digit:]]'
THEN CONCAT('t.`',c.column_name,'` AS `x',c.column_name,'`')
ELSE CONCAT('t.`',c.column_name,'`')
END
)
, ' FROM `',c.table_schema,'`.`',c.table_name,'` t;'
) AS stmt
FROM information_schema.columns c
WHERE c.table_schema = 'mydatabase'
GROUP BY c.table_schema, c.table_name
Again, there's a potential for generation "duplicate" column names with this approach. The conditional test "c.column_name REGEXP" could be extended to check for other "invalid" leading characters as well.
As a side note, at some point, someone thought it a "good idea" to name columns with leading digit characters. Just because something is allowed doesn't mean it's a good idea.
Then again, maybe all that rigamarole isn't necessary, and just wrapping the column names in backticks would be sufficient for your application.

I think you can follow option 2. However this will not be quick solution.
Another way around this could be,
Generate schema script for the tables you want to correct.
Open the script in notepad++ or any editor that supports find using regular expression.
Search and replace with [0-9]+_ expression and empty string for replacement.
Create the new tables using this script and copy data into them.
This may sound like a manual approach but you will do this once for all of your tables.

Look into a strategy of doing 2 selects, one for the column name, then one for the data with column alias. You might have to revert to some scripting language, like PHP, for help.
First, get the column names :
show columns from tbl_client;
+-------------------------------+-----------------------------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+-----------------------------------+------+-----+---------------------+-----------------------------+
| 1_X | int(11) | NO | PRI | NULL | auto_increment |
Then, loop through the results and create a list of column alias
Then create your new select
SELECT 1_X as NEW_COLUMN_NAME_FOR_FIELD_1 FROM tbl_client;

Related

Counting how many fields (in a row) are filled in SQL

I want to count how many columns in a row are not NULL.
The table is quite big (more than 100 columns), therefore I would like to not do it manually or using php (since I dont use php) using this approach Counting how many MySQL fields in a row are filled (or empty).
Is there a simple query I can use in a select like SELECT COUNT(NOT ISNULL(*)) FROM big_table;
Thanks in advance...
Agree with comments above:
There is something wrong in the data since there is a need for such analysis.
You can't completely make it automatic.
But I have a recipe for you for simplifying the process. There are only 2 steps needed to achieve your aim.
Step 0. In the step1 you'll need to get the name of your table schema. Normally, the devs know in what schema does the table reside, but still... Here is how you can find it
select *
from information_schema.tables
where table_name = 'test_table';
Step 1. First of all you need to get the list of columns. Getting just the list of cols won't help you out at all, but this list is all we need to be able to create SELECT statement, right? So, let's make database to prepare select statement for us
select concat('select (length(concat(',
group_concat(concat('ifnull(', column_name, ', ''###'')') separator ','),
')) - length(replace(concat(',
group_concat(concat('ifnull(', column_name, ', ''###'')') separator ','),
'), ''###'', ''''))) / length(''###'')
from test_table')
from information_schema.columns
where table_schema = 'test'
and table_name = 'test_table'
order by table_name,ordinal_position;
Step 3. Execute statement you've got on step 2.
select (length(concat(.. list of cols ..)) -
length(replace(concat(.. list of cols .. ), '###', ''))) / length('###')
from test_table
The select looks tricky but it's simple: first replace all nulls with some symbols that you're sure you'll never get in those columns. I usually do that replacing nulls with "###". that what all that "ifnull"s are here for.
Next, count symbols with "length". In my case it was 14
After that, replace all "###" with blanks and count length again. It's 11 now. For that I was using "length(replace" functions together
Last, just divide (14 - 11) by a length of a replacement string ("###" - 3). You'll get 1. This is exactly amount of nulls in my test string.
Here's a test case you can play with
Do not hesitate to ask if needed

How can I run a SQL UPDATE against the results of query that captures table names from INFORMATION_SCHEMA.TABLES?

I'm looking to replace a string in a CMS multi-site database across a common set of tables. Here is the initial query to collect the target tables:
SELECT TABLE_NAME as target_table
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%_content'
...against the results of which I'd like to run the following:
UPDATE target_table
SET title = replace(title, 'SEARCH_STRING', 'REPLACE_STRING')
WHERE title LIKE ('%SEARCH_STRING%');
Thanks in advance for the assist!
For a one off, a workable approach is to use SQL to generate a set of SQL statements.
Assuming that the table_schem and table_name don't contain backtick characters, and if your SEARCH_STRING and REPLACE_STRING don't contain single quotes (or are properly escaped), we could do something like this:
SELECT CONCAT('UPDATE `',t.table_schema,'`.`',t.table_name,'` c'
,' SET c.title = REPLACE(c.title, ''SEARCH_STRING'', ''REPLACE_STRING'')'
,' WHERE c.title LIKE (''%SEARCH_STRING%'') ;') AS `-- stmt`
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.table_name LIKE '%_content'
AND t.table_schema NOT IN ('information_schema','mysql','performance_schema')
ORDER BY t.table_schema, t.table_name
we can save the results from the query into a file. and then submit the SQL statements in the file to the MySQL server.
(I think I would be using INFORMATION_SCHEMA.COLUMNS, with tables containing column named 'title' as well as the table_name matching a pattern, but the approach is the same.
Note that this cannot be accomplished in a single SQL statement; the query to get the list of tables is going to have to be a separate statement, separated from the execution of the actual UPDATE statement(s).
EDIT I just took a look at the answer linked to in the question; that is totally unrelated. There's nothing there that would apply to the problem we are trying to solve here.
Because of the way SQL is processed (parse, syntax check, semantic check, determine execution plan, then execute) ... identifiers (e.g. table names) must be supplied as tokens in the SQL text. Identifiers cannot be supplied as values at execution time. That's why we need separate statements.

I want to insert a value multiple time in a same field without disturbing the previous data

eg : field name = User_id
Value=abc later i want to insert xyz without disturbing abc Value= abc,xyz i want to insert efg without disturbing abc then Value= abc,xyz,efg and so on
i want to seperating each value by using ","(comma). can any one help me out
In MySQL you could often refer to the value of a column just by using the column name. And to concatenate strings with a separator there's a nifty function called concat_ws (concat with separator).
In your case the code would look something like
UPDATE YourTable
SET Value = CONCAT_WS(',', Value, 'cde')
WHERE User_id = 123;
Good Luck!
MySQL CONCAT_WS() function is used to join two or more strings with separator. The separator specified in the first argument is added between two strings. The separator itself can be a string. If the separator is NULL the result is NULL.
Click hear for more information

Select all columns with ascii code in mysql

I have a code which I recently discovered :) and it does do its job and well done I might add. But, I want to check all columns instead of checking it by column. Is it possible
Check my code below:
SELECT column_name
FROM table_name
WHERE column_name REGEXP '[[.DLE.]-[.US.]]'
Now, what I want is something like this but it won't work
SELECT *
FROM table_name
WHERE REGEXP '[[.DLE.]-[.US.]]'
Kindly advice and I apologize for asking many questions :)
REGEXP is a binary operator which means you have to have a left operand and a right operand.
Like most arithmetic operators.
You could check all columns like this:
SELECT *
FROM table_name
WHERE CONCAT(a, b, c, d, ...) REGEXP '[[.DLE.]-[.US.]]'
I'm using ... for the sake of the example, but you'd need to name all your columns explicitly. There's no option to use a wildcard for the columns inside an expression.
You can't set "all columns" in a single SET clause. You'd need to do something like the following:
UPDATE table_name SET
a = REPLACE(a,char(16),''),
b = REPLACE(b,char(16),''),
c = REPLACE(c,char(16),''),
d = REPLACE(d,char(16),''),
...similar for other columns;
If you think this is an unexpected omission in the SQL language, then I wonder if you can name any other programming language that lets you compare to or assign a value to "all variables" in a single expression?

Full JOIN MySQL Query is returning empty

So here is a MySQL Query:
SELECT TestSite . * , LoggedCarts . *
FROM TestSite, LoggedCarts
WHERE TestSite.email = 'LoggedCarts.Bill-Email'
LIMIT 0 , 30
It is returning an empty result set, when it should be returning four results based on the tables below.
First Table: LoggedCarts - Column: Bill-Email
casedilla#hotmail.com
crazyandy#theholeintheground.com
Second Table: TestSite - Column: email
samuel#lipsum.com
taco#flavoredkisses.com
honeybadger#dontcare.com
casedilla#hotmail.com
messingwith#sasquatch.com
The goal is to get a MySQL statement that returns the rows in Table: TestSite that don't match the rows in Table: LoggedCarts.
Note: I understand that the use of a hyphen in a column name requires special care when constructing a query, involving backticks to tell MySQL there are special characters. I would change the column names to match up, however the Table: LoggedCarts has data fed via post from a Yahoo Shopping Cart and without heavy preparation before insertion setting the name to anything but the key sent in the post data is daunting.
However, if it turns out rebuilding the data prior to insertion is easier than using a JOIN statement or for some reason using two columns with different names as the comparison columns just doesn't work, I will go through and rebuild the database and PHP code.
Single quotes indicate a string literal. You need to use backticks for identifiers. Also, each component of an identifier must be quoted individually.
SELECT TestSite . * , LoggedCarts . *
FROM TestSite, LoggedCarts
WHERE TestSite.email = LoggedCarts.`Bill-Email`
LIMIT 0 , 30
From the manual:
If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.
With a bit of research inspired by somne of the hints given, I found the solution I was looking for here: SELECT * WHERE NOT EXISTS
Does exactly what I need it to do, and as a bonus, I like the shorthand syntax that is used that allows you to put in an alias for the table name and use the alias throughout the statement.
SELECT *
FROM TestSite e
WHERE NOT EXISTS
(
SELECT null
FROM LoggedCarts d
WHERE d.`Bill-Email` = e.email
)