I want to create a table / view from variable multiple table names that I get from a SELECT query.
It's possible to create a table from multiple known tables like so:
CREATE TABLE new_table AS
SELECT column_1, column_2
FROM clients_1, clients_2, ... clients_n;
To get list of tables I can use something like:
SELECT DISTINCT table_name FROM information_schema.columns WHERE table_name like '%clients_%';
Which returns:
table_name
1 clients_1
2 clients_2
How can I use the table names result as a list in CREATE TABLE FROM clause?
I tried something like this with a WITH:
WITH mytable AS
(SELECT DISTINCT table_name FROM information_schema.columns WHERE table_name like '%clients_%')
CREATE TABLE new_table AS
SELECT column_1, column_2
FROM mytable;
But it's mostly not working.
And even if it does, for example by not using WITH and selecting * columns -
CREATE TABLE new_table AS
SELECT *
FROM (SELECT DISTINCT table_name FROM information_schema.columns WHERE
table_name like '%clients_%');
new_table is just a copy of mytable/nested query.
Ideas?
Thanks!
I ended up going with a VIEW like so:
CREATE OR REPLACE VIEW clients_all AS
SELECT column_1, column_2 FROM client1
UNION ALL
SELECT column_1, column_2 FROM client2
The downside is I'll have to update the view whenever adding new client_n table,
And explicitly specify the name of the tables rather than inferring them from a query,
But I probably had to break it into two queries anyway even if creating a table as originally intended.
Related
I'm trying to auto-increment table-name while creating a new table.
Below one, is the generic way to do it.
CREATE TABLE table_name (column_name column_type);
How can we add an auto-increment to table name like: table_name1, table_name2, etc?
There is no "auto incrementing" functionality with table names, you should handle it yourself. You can, for example, count the tables in your database with specific names and in specific schema:
SELECT count(*) FROM information_schema.tables WHERE table_schema = 'YOUR_SCHEMA' AND table_name LIKE '%YOUR_TABLE_NAME%';
Increase the number you got in result and create a new table.
Thanks to #errata, I have solved this problem. If I have tables with names: table_name1, table_name2, table_name_100, table_name_120.
I wanted to add an increment to a new table. So in the first part, I fetched the max count of the alpha-numeric table name.
SELECT MAX(CAST(SUBSTR(TRIM(table_name),12) AS UNSIGNED)) FROM information_schema.tables WHERE table_schema = '${userSchema}' AND table_name LIKE '%table_name%';
It returns, for example, 120 in the above case.
Use this and increment with 1 to new table_name.
I want to exclude a column from select query. In this approach, i have to mention all the fields name except that i don't want. Is there any other approach?
SELECT column_1, column_2, column_3,
/* ...the list of columns 4-97, not shown in this example... */,
column_98, column_99
FROM table
Try this. As far as I remember, I got this solution from SO years back :)
SELECT * INTO #TMP FROM your_table
ALTER TABLE #TMP DROP COLUMN column_name
SELECT * FROM #TMP
--DROP TABLE #TMP WHEN PURPOSE IS DONE
I have two similar tables with same columns. I just want to add the data of the column of one table with the corresponding data of the column of another table. Thus, form a new table which consists of the sum of individual data of both the tables.
Would you please help me?
something like this?
INSERT INTO myTable
SELECT
s.salary + t.salary AS salary,
s.name
FROM table_name s
JOIN table_name_2 t ON t.name = s.name -- or whatever parameter you use to join the two tables on
you can do like
insert into newTable select * from table1;
insert into NewTable select * from table2;
this will insert two tables data to new Table.
I was wondering if you could create columns based on the result from a Select statement.
Table 1:
availableColumns
-----------------
column1 -> record 1
column2 -> record 2
column3 -> record 3
So if I did a Select availableColumns From Table1, how could I create a table that has then the following structure, the results must be used to create columns:
column1 | column2 | column3
If I try:
CREATE TABLE test SELECT availableColumns FROM table1
I get the following:
Column
------
Column1
Column2
Column3
So instead of columns, I get my result as rows which I don't want.
Thanks in advance :)
You can have a list of columns for a specific table by selecting them from Information Schema table
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='databasename'
AND `TABLE_NAME`='tablename';
To create the table just use it
CREATE TABLE test SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='databasename' AND `TABLE_NAME`='tablename';
Just change 'databasename' and 'tablename' to fit your actual database and table
Two questions:
1)
There are several tables that are used as an archive for other tables.
To do so, there is a
INSERT INTO data_archive_table (SELECT * FROM data_table)
The problem is that the data_table.id should be kept as data_archive_table.old_id.
Is there a way to write a query that will look like: SELECT *, id AS old_id FROM data_table, while the results columns will have ONLY the old_data column, and NOT the original id column?
Using all column names is the only option I see, but I prefer to avoid it.
2)
I want to add a virtual column named deleted_time to the insertion query, that will hold the current time.
Can it be done? if so - how ?(tutorials will be great)
Try this:
1.) You can use something like this query:
INSERT INTO data_archive_table
SELECT id AS old_id -- be sure that data_archive_table has column oldID
,... -- You need to specify the names of the columns
FROM data_table
WHERE id = 'IDHERE' -- If you want to have condition.
2.) For this, you can add the value directly in you select statement
INSERT INTO `tableName`
SELECT colA,
colB,
, ...
, NOW() as deleted_time -- NOW() is a function in MySQL
FROM `sourceTable`
WHERE colA = 'IDHERE' -- If you want to have condition.
NOW() in MySQL