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
Related
I want to search for a single value in 3 columns and return each occurrence of this value.
My query is based on the following answer regarding a similar request.
SELECT *
FROM table
WHERE 'abc' IN (column1, column2, column3)
However, I don't want to return the whole row, only the single value. It possible that the value can found in multiple columns for the same row. Each occurrence should be returned and the end result should be a 1-dimensional list. How do I need to alter my query?
The value that you want returned in each row is the same as the value that you search for, so a statement like this will do:
SELECT 'abc' AS column_name
FROM table
WHERE 'abc' IN (column1, column2, column3)
If you want 'abc' returned once for each occurrence in any of the 3 columns you should use UNION ALL:
SELECT column1 AS column_name
FROM table
WHERE column1 = 'abc'
UNION ALL
SELECT column2 AS column_name
FROM table
WHERE column2 = 'abc'
UNION ALL
SELECT column3 AS column_name
FROM table
WHERE column3 = 'abc'
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.
I want to show column expect column number 1
for example = select * from table; this query will show all column
column1 column2 column3 ...
but my question is how to show all column expect column1, so the result will be like this
column2 column3 ...
I don't want to use select column2, column3 from table because that query not efective if my column more than 3. thank
You can select the column after SELECT:
SELECT * FROM table
* means ALL columns, so:
SELECT columnName, columName2 FROM table
I have a MySql table which has about 100k rows. there is one field say id which contains numbers from 1-35. all these records fall in this range of id i.e. all these records have value of id column between 1-35.
Now i want to create another table which will have one row of each id. i.e the new table should have 35 rows only.
How to go about it ?
create table new_table (id int);
insert into new_table
select distinct id from big_table;
Edit:
You can create the new_table by outputting the big_table create script and changing the name.
SHOW CREATE TABLE big_table;
/* modify the name of the output and execute */
insert into new_table
select * from big_table group by id
You have a table with 100.000 rows, and you want a new table with 35 rows. What values do you want for the remaining columns?
If the answer is: doesn't matter, this works:
CREATE TABLE newTable
SELECT * FROM yourTable
GROUP BY ID;
If you only want the IDs,
CREATE TABLE newTable
SELECT DISTINCT ID FROM yourTable;
You can copy data from one table to another even difference database(Schema) as following
INSERT INTO [DestDatabase].[DestTablName]
SELECT [ColumnName] FROM [SourceDatabase].[SourceTablName];
So, you can use two way:
1:
INSERT INTO tbl_New
SELECT DISTINCT id from tbl_Original;
2:
INSERT INTO tbl_New
SELECT id from tbl_Original GROUP BY id;
I use this query to select fields in a given table. Is it possible to select only the fieldname and not the whole structure of the table?
SHOW COLUMNS FROM student
You're trying to determine the table structure? You can query MySQL's information_schema database directly for the fieldnames:
select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='student';
The solution mentioned here earlier is not the correct one. Example:
CREATE DATABASE db1;
CREATE DATABASE db2;
CREATE TABLE db1.t ( id_1 INT);
CREATE TABLE db2.t ( id_2 INT);
SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME ='t';
This will display:
+-------------+
| COLUMN_NAME |
+-------------+
| id_1 |
| id_2 |
+-------------+
suggesting that the table t has two column which is obviously not true. This query lists all the columns of the tables called t in all of your databases.
Instead, you should specify which database contains the table t you want to select the column names from:
SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE
TABLE_NAME = 't' AND
TABLE_SCHEMA = 'db1';
select COLUMN_NAME FROM TABLE_NAME
FOR EXAMPLE: ROLLNO is a column_Name of table Student....
select ROLLNO from Student