List of temporary table columns (MySQL) - mysql

I need to get list columns of some temporary table (MyISAM) in MySQL in view like number column - name column.
I need to know number of column with specific name. In advance, I can't know what is the number of column - I'm using dynamic sql with some variables to create temporary table.
I can not use show columns... because I can't work with results of this function. I can't use INFORMATION_SCHEMA.COLUMNS because it does not contains columns of temporary table.
Some ideas?
Thanks in advance!

Related

Create a table and specify data types according to an existing query

Let's say I have a table like this
this table is the result of a query from another larger table stored in my database
All I want is to create a table like this one above and specify for each column a custom format and store it into my database
I know that I could do create table mytab as select ... etc
however i don't know how to specify the column formats that I want in mysql
could you please help ?
If you have the query sql, you should be able to do a select into to store the results in a table. Add a LIMIT clause to just store one row. You could then do SHOW CREATE TABLE tablename (from this SO answer) to get the SQL for creating the table. It would be up to you to figure out what your primary key should be.
Assuming with column formats you mean data types: Use CAST to cast to the desired data type.
create new_table as
select
cast( a.metrique as varchar(100) ) as metrique,
cast( b.nombre_de_lignes as int ) as cote_de_lignes, ...
from ...
You may specify columns properties completely or partially. Like there is no SELECT part, and you simply create empty table.
I.e. like
CREATE TABLE table_name ({any definitions allowed in table creation query:
columns specifications, indices, constraints, FKs, etc.})
SELECT ...
In this form each output column in SELECT must have alias which matches according column name defined in CREATE TABLE part. If alias is absent in the structure then a column with the name==alias will be added to the table definition with dynamically formed properties.

how to create a new table from `describe` statement in sql?

I want to create a new table with rows of the column names. Essentially, I want to put the output of a decribe statement into a new table, so that I can use a where clause to only extract certain column names. I'm using sparksql.
How do I do this? Thanks.
No need to create an additional table just use either SHOW COLUMNS command instead of describe with filter, or query information_schema.columns table with a select.

SELECT all COLUMN_NAMES from a table that have a specific entry

I have a table with 21 columns and I need to find all column names that have a specific value in them. This goes beyond my knowledge of database querying.
If this is possible, how would I do this?
Do you need to find columns in which the name of the column contains a certain value or there exists a row in which the given column contains the given value?
In the first case you could try the show columns: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html

Select count from each table from a list stored on a table

I have a table tbls with a field name containing names of tables.
I'm trying to form a statement to get the number of rows of each of this tables.
Should I use a stored procedure or is there a simpler way to do it?
There is a thread that talks about how you can Get record counts for all tables in MySQL database. Using the result, you can constraint the result set to tables specified in the tbls table.

Add new field to multiple table at once?

I want to add same field to multiple tables. Can I do this with single query in MySql?
I don't think you can do this with a single query, as it requires the ALTER TABLE syntax which only supports one table per query (although you could add/modify multiple columns in the same table).
You have to do one ALTER TABLE query per table to be modified.