SQL/MySQL - Skip or no select a column from select query - mysql

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

Related

Athena SQL create table from variable number of tables

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.

How to re-order a table with a certain value at the top

For example, I have a table like this:
id
|02|
|01|
|03|
|05|
|04|
I need to alter the table with 05 at the top:
id
|05|
|01|
|02|
|03|
|04|
SELECT * FROM table1 ORDER BY id=05 desc,id works. But I want to alter the table.
ALTER TABLE table1 ORDER BY id can re-order the table data. But
ALTER TABLE table1 ORDER BY id=05 desc,id does not work.
I have an application which the old version does not order the table, so it displays '02' on top. I cannot update the application any time soon, so I have to re-order the table instead.
ALTER table isn't used to manipulate data, it's used to modify table structure. Your best bet would be to SELECT into a temp table, TRUNCATE, then re-insert from the temp table.
Something like:
SELECT *
INTO #temp
FROM table1
ORDER BY id=05 desc,id;
TRUNCATE table1;
INSERT INTO table1
SELECT * FROM #temp;
DROP #temp;
I can't find a way to do this without changing the structure of the table. UPDATE works with this kind of ordering, so I have to insert a new column, update it with a variable and alter it
ALTER TABLE table1 ADD COLUMN id2 INT NULL;
SET #i=1;
UPDATE table1 SET id2=(#i:=#i+1) ORDER BY id=05 desc,id
ALTER TABLE table1 ORDER BY id2;

Create new table with calculated data from another table

I'm going to do an regression analysis through my data chunks. For that I need to find out various values. For each data set I need to get N:count(X) sumX sumY sumX*X etc.
Separately I wrote queries for those operations like
SELECT COUNT(X) FROM table_name
SELECT SUM(X*X) FROM table_name
I need to create another table which a row contain count(X), sumX , sumX*X etc. How can I write that kind of query?
You can add multiple aggregates to the same query and use create table as:
create table yournewtable as
select count(x) cnt, sum(x*x) sumxx, sum(x) sumx
from table_name
SQL Fiddle Demo
This will return you a single row. If you need to break it apart, look into group by.
CREATE TABLE first and then use INSERT INTO
CREATE TABLE yourTableName
(
col1 int,
col2 int,
col3 int
);
INSERT INTO yourTableName (col1, col2, col3)
SELECT
(SELECT COUNT(X) FROM table_name),
(SELECT SUM(X) from table_name),
(SELECT SUM(X*X) from table_name)

Any MySQL command to duplicate rows except for auto_increment column?

I have been looking for a way to duplicate a row, and insert it back to the table, but with a different id value (whose type is auto increment).
I could do this by specifying every column manually, but as there are many columns, and as the columns can be added or removed in the future, I want to use some easy query to do this without having to specify every column name.
Try this:
CREATE TEMPORARY TABLE temp_table SELECT * FROM source_table WHERE ...;
ALTER TABLE temp_table DROP COLUMN column_with_auto_increment;
INSERT INTO source_table SELECT * from temp_table; DROP TABLE temp_table;
Try
INSERT INTO new_table (attr1, attr2, attr3) SELECT oldatr1, oldatr2, oldatr3 FROM old_table WHERE <the filter you want>
It also may work if new_table and old_table are the same.

How to store MySQL query results in another Table?

How to store results from following query into another table. Considering there is an appropriate table already created.
SELECT labels.label,shortabstracts.ShortAbstract,images.LinkToImage,types.Type
FROM ner.images,ner.labels,ner.shortabstracts,ner.types
WHERE
labels.Resource=images.Resource
AND labels.Resource=shortabstracts.Resource
AND labels.Resource=types.Resource;
If the table doesn't exist (and you e.g. don't want to create it because it may have lots of column names) you can create it on the fly...
Query:
CREATE TABLE another_table SELECT /* your query goes here */
You can use the INSERT INTO TABLE SELECT....syntax:
INSERT INTO new_table_name
SELECT labels.label,shortabstracts.ShortAbstract,images.LinkToImage,types.Type
FROM ner.images,ner.labels,ner.shortabstracts,ner.types
WHERE labels.Resource=images.Resource AND labels.Resource=shortabstracts.Resource
AND labels.Resource=types.Resource;
if your table dosen't exist then
CREATE TABLE new_table SELECT //write your query here
if your table exist then you can just insert query
INSERT INTO new_table SELECT //write your query here
For more check here and here
INSERT INTO another_table SELECT /*your query goes here*/
In SQLite Studio, I noticed that "AS" keyword is needed:
Query:
CREATE TABLE another_table AS SELECT /* your query goes here */