INSERT ... SELECT to same table - mysql

I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.

Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL

Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.

Related

Trying to copy or somehow move the contents (values) in a TEXT column 3k+ large rows to another table in the same database without success

I have created a new column in the "destination" table with the same name, datatype and other values as appear in the "source" column in a different table. I have tried many suggested solutions as found on stackoverflow. This one appeared to work (found on Quora) but when I went to the destination table the previously empty column remains empty with nothing but NULL values noted. This is the Quora suggestion:
you can fill data in column from another existing one by using INSERT INTO statement and SELECT statement together like that
INSERT INTO `table1`(column_name)
SELECT column_name FROM `table2`
here you filled a single column in table 1 with data located in a single column in table 2
so if you want to fill the whole table 1 (all columns) with data located in table 2 you can make table 1 like a copy of table 2 by using the same code but without column name
INSERT INTO `table1`
SELECT * FROM `table2`
but note to do this (copy table content to another one) ensure that both of tables have the same column count and data types.
I'm not sure what is meant by column count (do the two table have to have the same number of columns?)
When I run it I get error # 1138.
Any help greatly appreciated. -JG

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.

Create as Select and add an extra column

I want to create a MySQL table as a copy of another table like this:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
The twist is that I want, if possible, to add at the time of creation another empty column, that will be populated at a later time.
I know that I can just create it as above and use ALTER TABLE afterwards, but my thinking is that, given a large amount of data, the ALTER is gonna take a long time (please contradict me if this is wrong), that can be saved if what I want is possible.
So, say I want an extra extra_col - varchar(64), what would my original query be?
Thanks.
As documented under CREATE TABLE ... SELECT Syntax:
You can create one table from another by adding a SELECT statement at the end of the CREATE TABLE statement:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
MySQL creates new columns for all elements in the SELECT.
[ deletia ]
Notice that the columns from the SELECT statement are appended to the right side of the table, not overlapped onto it.
[ deletia ]
In a table resulting from CREATE TABLE ... SELECT, columns named only in the CREATE TABLE part come first. Columns named in both parts or only in the SELECT part come after that. The data type of SELECT columns can be overridden by also specifying the column in the CREATE TABLE part.
Therefore:
CREATE TABLE new_tbl (
extra_col VARCHAR(64)
) SELECT * FROM orig_tbl
#user1703809 You have a workaround if you want to place the added column at the end by placing the extra column in the Select statement as :
CREATE |TEMPORARY| TABLE IF NOT EXISTS new_tbl
SELECT *, REPEAT('-',64) extra_col
FROM orig_tbl;
This will create your new table with an added column "extra_col" - varchar(64). Furthermore, you may create the table as temporary if it suits you, and if you just want to create an empty table for further use, just add a "LIMIT 0" at the end of the statement.
Furthermore, this way, you may add a column in any position of the field list at the Select statement.
It's been sometime since you asked the question but I'm still hoping to be of some help.

MySQL Column wise Insert

I am trying to create a calculation logic using MySQL tables.
Data from two table is processed using a stored procedure and a set of values is generated.
These values are part of a column of output table.
I have to run different procedure to generate output for each column in output table
Now if I create insert query for each row it will have large number of inserts for each column. Can I insert a set of values into a table column in one go? assuming other columns can be NULL.
INSERT INTO tableName(columnName)
VALUES ('baz'),('foo'),('bar'),('baz'),('baz'),('baz'),('baz');
etc as u like..
See this: Bulk insert into table with one single query
The insert can be done for one column rest can be NULL if remaining columns are nullable.
But next time for the remaining columns the Insert will not work for the existing rows. If You want to update the existing rows then you need to fire the update query.
Assuming col1 and col2 are nullable
If you want to insert in col1 keeping col2 null insert will work
If you want to insert in col2 keeping col1 null insert will work

modify table with column with null values

what if I wanted to update the records in the table by altering values in one of the columns?
I have records in the table that have one column empty(null values). I want to change these values and insert values from another table into those records.
Basically I have a table with one column empty. I do not want to append to the end of the table but start inserting from record 1.
For the existing records, you would have to use UPDATE to update that one column, WHERE thatColumn IS NULL.
Shouldn't the values in that column have some relation to the rest of the record? I could understand initializing the existing records to a non-null value, or using an UPDATE query to populate data from another table in that column, but all related to the original row...
UPDATE old SET old.badColumn = new.newData
FROM oldTable old
JOIN newTable new on old.someID = new.someID
This would find the related data in newTable matching oldTable, and update the badColumn to some data from newTable... let me know if you need more help.
See the "Using the UPDATE statement with information from another table" section from this page of SQL Server Books Online.