How to select a table in query as variable in mysql? [duplicate] - mysql

This question already has an answer here:
How to select from MySQL where Table name is Variable
(1 answer)
Closed 6 years ago.
I have mutliple tables with same name with added date in the last as abc_2016_09.
Now How can i call this mentioned statement, pls help.
SET #var = concat('ABC_table','_',date_format(curdate(), '%Y'),'_',date_format(curdate(), '%m'));
AND THEN
select * from #var
error code 1069 is coming.

SET #var = concat('abc_table','_',date_format(curdate(), '%Y_%m'));
SET #statment = concat('select * from ', #var);
PREPARE stmt FROM #statment;
EXECUTE stmt;
Check it in mysql workbench, It does not return row in mariadb.

Related

How to query through all varchar columns for a specific char [duplicate]

This question already has answers here:
Search text in fields in every table of a MySQL database
(27 answers)
Closed 3 years ago.
I have to search for the char ' - ' in all columns (and lines) of my table. Currently I have this:
SELECT *
FROM `my_table`
WHERE (
SELECT C.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.TABLE_NAME = 'my_table'
) LIKE('%-%');
But, of course, this doesn't work because of the multiple values returned by the subquery. Any ideas of how to achieve this?
You can get the query with a SELECT on INFORMATION_SCHEMA.COLUMNS and execute the query with a Prepared SQL Statement on MySQL:
SET #query = '';
SELECT CONCAT('SELECT * FROM table_name WHERE ',GROUP_CONCAT(C.COLUMN_NAME SEPARATOR ' LIKE ''%-%'' OR '), ' LIKE ''%-%''') FROM INFORMATION_SCHEMA.COLUMNS C WHERE C.TABLE_NAME = 'table_name' INTO #query;
PREPARE stmtSearch FROM #query;
EXECUTE stmtSearch;
DEALLOCATE PREPARE stmtSearch;
demo on dbfiddle.uk

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;

Exclude two column from in select statement php [duplicate]

This question already has answers here:
Exclude a column using SELECT * [except columnA] FROM tableA?
(47 answers)
Closed 7 years ago.
I am having a table that contains 61 columns.
In the select statement I want to fetch all the columns except two of them.
How can I do it?
Here is the code that fetches data from table:
function hotelfeature($id)
{
global $conn;
$selFeature = "select * from hotelpropertyoptioninfo where Hotel_id = " .$id;
$resultFeature = mysql_query($selFeature,$conn);
$rowFeature = mysql_fetch_assoc($resultFeature);
return $rowFeature;
}
SET #SQL = CONCAT('SELECT ', (SELECT GROUP_CONCAT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '<table>' AND COLUMN_NAME NOT IN ('field1','Field2')),
' FROM <table>');
PREPARE stmt1 FROM #SQL;
EXECUTE stmt1;

MySQL select query [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
MySQL wildcard in select …
SELECT icon_* FROM images WHERE 1
I have three fields, icon_small, icon_big, and icon_large. How do I get all three without manually specifying them?
As far as I know, you can't. You will have to manually specify them.
(See the duplicate)
You have to specify them in your SELECT, but you can select a list of columns (which can then only be used in dynamic SQL) by doing:
select column_name from information_schema.columns
where table_schema = database()
and table_name = 'mytesttable'
and column_name like 'icon_%'
set #qry = (select concat('select ',group_concat(column_name), ' from ' ,table_name) from
information_schema.columns
where table_schema = database()
and table_name = 'your_table_name'
and column_name like 'icon_%');
prepare stmt from #qry;
execute stmt;
deallocate prepare stmt;

convert mysql stored procedure to mssql

I need to used dynamic order query in mysql and i have successfully achieved that through string concatenation in mysql as follows:
set #stmt_text := concat('select * from abc order by ',sorder);
prepare stmt_handle from #stmt_text;
execute stmt_handle;
deallocate prepare stmt_handle;
i need a similar way to convert this in mssql
Any ideas??
Yes, just run it like this:
execute ('select * from abc order by ' + #sorder);
But don't forget that you need to verify the sorder variable if you get it through user input (to stop sql-injections)