MySQL: alias column name question - mysql

Is it possible to alias a column name with the result of a simple SELECT query.
This doesn't work:
SELECT `hlevel1` AS (SELECT `level1` FROM `hierarchy_labels` LIMIT 1) FROM `hierarchy`;
Any Suggestions?

You can't do this.
Aliases are used to rename a field or to name a calculated field.
If you simply want your results to be named 'hlevel1', you may want to try this:
SELECT level1 as hlevel1 FROM hierarchy_labels LIMIT 1

Use a prepared statement.
SELECT `level1` INTO #x FROM `hierarchy_labels` LIMIT 1;
SET #s = CONCAT('SELECT `hlevel1` AS `', #x, '` FROM `hierarchy`');
PREPARE s FROM #s;
EXECUTE s;
DEALLOCATE PREPARE s;

Related

How to call MySQL function for every column that is present in table?

I need to call a mySQL function for all columns in a table.
I know how to do it for a particular column
Like this:
UPDATE `table_name` set `column_name` = function_name(`column_name`)
But i have no clue how to do it for all columns at once.
Thanks in advance.
Little clarification: I dont want to manually mention all columns, as i probably could have 200 columns table.
But i have no clue how to do it for all columns at once.
You just can't - there is no such shortcut in the update syntax.
You can do this with a single update statement, but you need to enumerate each and every column, like:
update table_name set
column_name1 = function_name(column_name1),
column_name2 = function_name(column_name2),
column_name3 = function_name(column_name3)
An alternative would be to use dynamic SQL to programatically generate the proper query string from catalog table information_schema.columns, and then execute it. This seems uterly complicated for what looks like a one-shot task... But here is sample code for that:
-- input variables
set #table_schema = 'myschema';
set #table_name = 'mytable';
set #function_name = 'myfunction';
-- in case "GROUP_CONCAT()" returns more than 1024 characters
set session group_concat_max_len = 100000;
-- build the "set" clause of the query string
select
#sql := group_concat(
'`', column_name, '` = ', #table_schema, '.', #function_name, '(`', column_name, '`)'
separator ', '
)
from information_schema.columns
where table_schema = #table_schema and table_name = #table_name;
-- entire query string
set #sql := concat('update ', #table_schema, '.', #table_name, ' set ', #sql);
-- debug
select #sql mysql;
-- execute for real
prepare stmt from #sql;
execute stmt;
deallocate prepare stmt;

use result string from one table as column names for another query

I am trying to trying to simplify the following query :-
SELECT id, m_field_id_46 AS Liverpool,m_field_id_47 AS London,m_field_id_48 AS Belfast FROM member_data
In a way i can dynamically create the column names
SELECT id, (SELECT GROUP_CONCAT('m_field_id_',m_field_id,' AS ',m_field_label) FROM member_fields) as dist FROM member_data
However this is not working. Please help
i got it working by looking at another answer from stackoverflow: -
SET #listStr = ( SELECT GROUP_CONCAT('md.m_field_id_',m_field_id,' AS `',m_field_label,'`') FROM member_fields );
SET #query := CONCAT('SELECT ', #listStr, ' FROM member_data');
PREPARE STMT FROM #query;
EXECUTE STMT;

MySQL: Match against dynamic values

I have a list of values in a table column that I need to match against table names, preferably just using an SQL statement.
If the values were static, I suppose the SELECT statement would be something like this:
SELECT table_name FROM information_schema.TABLES WHERE
match(table_name) against('124512' +'36326' +'23636' IN BOOLEAN MODE)
However, I need to match against dynamic values coming from a SELECT statement:
SELECT tableid FROM databaseName.tableOverviewTableName
WHERE template = 'templateName')
The tableid above is contained in the table_name for the tables that I want.
Is this possible to achieve with an SQL statement?
You can do this via Prepared statement (not directly via a query)
SET #tq = (SELECT tableid FROM databaseName.tableOverviewTableName WHERE template = 'templateName'));
SET #stmq = CONCAT('SELECT * FROM ', #tq);
Prepare stmt FROM #stmq;
Execute stmt;
DEALLOCATE PREPARE stmt;

Using a SELECT query as column name in MySQL

I would like to get the values of the auto_increment column in my table (example). The catch is however, that I don't have the name of the auto_increment field. I'm currently using the following query to determine the name of the field:
SELECT column_name FROM information_schema.columns WHERE table_name = 'example' AND extra = 'auto_increment' LIMIT 1;
I would now like to pass the result of this query, as a 'string' to my actual query, and get the value. If I would like to do this in one go, how would I do that, because the below query, which should give me all auto_increment values used, only yields the above result -namely the auto_increment column name.
SELECT (
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'example'
AND extra = 'auto_increment'
LIMIT 1
) AS pri
FROM example
Any thoughts would be appreciated :)
Many Regards,
Andreas
Here is an example of how you would do this using prepare and execute:
SELECT #s := concat('select `', column_name, '` from example e')
FROM information_schema.columns
WHERE table_name = 'example' AND extra = 'auto_increment'
LIMIT 1
prepare stmt from #s;
execute stmt;
deallocate prepare stmt;

MySQL Variable not working

I tried to learn about MySQL Variable and do command like this
SET #target=`name`;
SELECT #target FROM transaction_product LIMIT 10;
But it is error and said Unknown column 'name' in 'field list'
Why it is error, i'm sure there is column name on my field list
here is the screenshot of the table
you need to use different quotes 'name' for assigning string to a variable and `name` for column names:
SET #target='name';
to get column value you can use INTO clause:
SELECT `name`
INTO #target
FROM transaction_product
LIMIT 1;
to get multiple rows in single variables you can use GROUP_CONCAT:
SELECT GROUP_CONCAT(`name`)
INTO #target
FROM transaction_product
LIMIT 10;
to execute query dynamically:
SET #target='`name`';
SET #query1 = CONCAT('
SELECT ',#target,'
FROM transaction_product
LIMIT 10'
);
PREPARE stmt FROM #query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SELECT #target:=`name` FROM transaction_product LIMIT 10;