MySQL Variable not working - mysql

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;

Related

How to set a local list/tuple variable in mysql

Is there a way to do the following in mysql?
SET #studios = ('Disney', 'Warner Bros.', 'Fox');
SELECT * FROM movies WHERE provider IN #studios;
When I try doing the above I get the error:
Operand should contain 1 column(s)
The error is coming from your initial assignment. You cannot assign lists to variables.
The only way of doing this in MySQL is to either create a temp table to hold the values, and then do ... IN (SELECT someVal FROM thatTemp), or to dynamically create the query with the values directly in the query string.
Example temp table creation:
CREATE TEMPORARY TABLE `someTemp` ( someVal VARCHAR(16) );
INSERT INTO `someTemp` (someVal) VALUES ('a'), ('b'), ('c');
SELECT * FROM myTable WHERE myField IN (SELECT someVal FROM someTemp);
DELETE TEMPORARY TABLE `someTemp`;
Alternatively, there is also FIND_IN_SET, which could be used like this:
SET #list = 'a,b,c';
SELECT * FROM myTable WHERE FIND_IN_SET(myField, #list) <> 0;
but this method probably has extremely poor performance (and may not be useable if your "myField" values may contain commas).
It is not possible to set a tuple/list/array in a user-defined variable in MySQL. You can use Dynamic SQL for the same:
-- we use single quotes two times to escape it
SET #studios = '(''Disney'', ''Warner Bros.'', ''Fox'')';
-- generate the query string
SET #query = CONCAT('SELECT * FROM movies WHERE provider IN ', #studios);
-- prepare the query
PREPARE stmt FROM #query;
-- execute it
EXECUTE stmt;
-- deallocate it
DEALLOCATE PREPARE stmt;
You could concatenate your list to a string, and use FIND_IN_SET as your criteria. Might not be super efficient, but makes the code quite easy to read and maintain.
Looks like this:
SET #studios = CONCAT_WS(',',
'Disney',
'Warner Bros.',
'Fox'
);
SELECT * FROM movies
WHERE FIND_IN_SET(provider, #studios) <> 0;

using a variable mysql not working as expected?

It doesn't like the limit line below. How would I use the variable #row in this case to limit the result set?
SELECT #row := 5;
SELECT * FROM MyTable
limit #row
Error:
Unexpected '#row'
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants https://dev.mysql.com/doc/refman/8.0/en/select.html
So,
SELECT * FROM MyTable
limit 5
You could use a prepared statement...
SET #row = 5;
SET #s = CONCAT('SELECT * FROM MyTable LIMIT ', #row);
PREPARE stmt FROM #s;
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: alias column name question

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;