Interpreting the value of TEXT as a part of a query? - mysql

Consider this example below in MySQL.
DECLARE myText TEXT;
SET myText = "myColumn";
SELECT * FROM myTable WHERE myText = "myValue";
In the above scenario, it wouldn't select any columns, because the value of myText is never "myValue". However, what I really want to do is to interpret the value of myText as a part of the query, so that the final query that it executes is:
SELECT * FROM myTable WHERE myColumn = "myValue";
How can I do that?

Use prepared statements
SET #myText = "myColumn";
SET #s = CONCAT('SELECT * FROM myTable WHERE ', #myText, ' = "myValue" ');
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

Related

mysql - Using Concat In Stored Procedure Dynamic Query

To do point, I have simple query like
SELECT * FROM mytable WHERE concat(firstName, ' ', lastName) in ('Adan Jack');
Query above run smoothly. But how if I combine that condition using Dynamic Query that using concat before?
I did this:
BEGIN
set #cond = concat(concat("firstName"," ", "lastName"), " in ('Adan Jack')";
set #query = concat("SELECT * FROM mytable WHERE ", #cond);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
But no result and cause error.
Thanks.

select list of columns dynamically in MySQL

I've looked through a lot of posts on this site and others and I can't figure this out. I'm trying to select a list of columns from a table and then use them in a query similar to this:
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
set #s = 'select ' + #cols + ' from REPORTS.t_labor';
prepare stmt from #s;
execute stmt;
deallocate prepare stmt;
-- execute ('select ' + #cols + ' from REPORTS.t_sales');
I am trying to run the above statements as-is. I also tried creating a stored procedure, but I have never worked with stored procedures in MySQL before and I don't know how to debug them. I would prefer to do this without using a stored procedure if possible, but if it's necessary that's ok.
I tried using the prepare statement, and I tried the execute statement but I couldn't get either to work.
Wild guess here, but try this.
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
PREPARE stmt1 FROM 'select ? from REPORTS.t_labor';
EXECUTE stmt1 USING #cols;
DEALLOCATE PREPARE stmt1;
Finally figured it out:
set #cols = (select group_concat(column_name) as 'col_list' FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'REPORTS' AND `TABLE_NAME` = 't_labor' AND column_name like '%host%'));
set #qry = concat('select ', #cols, ' from REPORTS.t_labor');
PREPARE stmt1 FROM #qry;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Had to piece together replies from at least 5 different posts similar to this question plus Dr OSWaldo's reply.

Paramater for name of a table without using prepare statement in MySQL

can i somehow set parameter for name of table in query without using a prepare statement?
This is example:
SET #tableName = 'Customer';
SELECT * FROM #tableName;
Thanks
Depending on the version of MySQL you are using, you may be able to use something like:
SET #tableName = 'Customer';
SET #s = CONCAT('SELECT * FROM ', #tableName);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

How to create a user defined variable that can be used in a set expression?

I would like to do this
set #a =(1,2,3);
select * from mytable where somefield in #a;
But mysql does not like it.
How can I do this?
One way to go about this
SET #a = '1,2,3';
SELECT *
FROM mytable
WHERE FIND_IN_SET(somefield, #a) > 0;
Note: This will effectively cause a full scan.
IMHO you better off without user variables containing strings
SELECT *
FROM mytable t JOIN
(
SELECT 1 somefield UNION ALL
SELECT 2 UNION ALL
SELECT 3
) q
ON t.somefield = q.somefield;
One more option is to leverage dynamic SQL
SET #a = '1,2,3';
SET #sql = CONCAT('SELECT * FROM mytable WHERE somefield IN(', #a, ')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Here is SQLFiddle demo for all queries
You can do this using temporary tables, as in this question:
How can I simulate an array variable in MySQL?
Or you can use find_in_set() as in this question:
how to set an array as a mysql user variable
It could be done but with proper syntax and prepare statement, likethis:
set #a =1,2,3;
select * from mytable where somefield in (#a);
set #crt=concat("select * from mytable where somefield in ",concat('(',#a,')'),";");
prepare smnt from #crt;
EXECUTE smnt;
DEALLOCATE PREPARE smnt;
Try this
SET #a = '1,2,3';
SET #sql = CONCAT('SELECT * FROM test WHERE `user` IN(', #a, ')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
See Fiddle Demo

How to select from MySQL where Table name is Variable

I have a case where getting the table name should be from a set variable like:
SET #ID_1 = (SELECT ID FROM `slider` LIMIT 0,1);
SET #Cat = (SELECT Category FROM `slider` LIMIT 0,1);
select * from #Cat where ID = #ID_1
but doing that way MySQL outputs an error, so could someone show me how I can achieve that, because these are my baby steps in MySQL.
You'd have to do this with a prepared statement. Something like:
SET #s = CONCAT('select * from ', #Cat, ' where ID = ', #ID_1);
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;