I want to copy all rows from table and insert this rows in itself this table, that is if in table are rows
column
--------
A
B
I want obtain
column
--------
A
B
A
B
This may be make with php (or other language) right? but I interest, possible make this as 1 query? own mysql can this?
Try this:
INSERT INTO `table` ( `column` )
SELECT `column` FROM `table`;
Related
Is this possible? For some context I am working with temporary tables and would like to shift values around. I am simply wondering if I can insert a value from a column of one table into the column of a new table, while deleting the original value from the first table (if that makes sense).
Table 1
|id| |Column_with_value|
---------------------------------
1 | blah |
---------------------------------
Table 2
(empty)
becomes:
Table 1
(empty)
Table 2
|id| |Column_with_value|
---------------------------------
1 | blah |
---------------------------------
I don't know one single operation which can do this. But there is really no need to put that into one operation as long as it stays in one transaction:
BEGIN;
SELECT * FROM table1 …;
UPDATE table2 …;
DELETE FROM table1 …;
COMMIT;
You can use a trigger on table 1 which listens for delete statements.
Something like this:
CREATE TRIGGER `my_insert_table2_trigger`
AFTER DELETE ON `table1`
FOR EACH ROW
BEGIN
INSERT INTO `table2` VALUES (OLD.id, OLD.Column_with_value)
END
With OLD you can access the values of the deleted row.
Try like this
CREATE TRIGGER Remove_Old
BEFORE DELETE ON Table_1
FOR EACH ROW
Insert Into Table_2
SELECt * From Table_1
Moving data between tables costs, complicates schema, and increases maintenance. Probably better if you had a single table with all of the data, with an additional column to tag it accordingly.
However
INSERT INTO tbl_1 (id, column)
SELECT id, column from tbl_2 where id=1;
DELETE FROM tbl1 where id=1;
Or if you just want to move everything from a full table to an empty table, try
RENAME TABLE tbl_1 TO tbl_2
I have searched for the answer to this and cannot find it anywhere. Not quite how I want anyway.
I have five Mysql columns in a table, that I want to combine to make one column.
column1value | column2value | column3value | column4value | column5value
Needs to become
column1valuecolumn2valuecolumn3valuecolumn4valuecolumn5value
In one column (Column 1). And I need this to happen for every row.
Many thanks in advance.
If you just want to retrieve the data combined in that fashion:
SELECT CONCAT(
column1value,
column2value,
column3value,
column4value,
column5value
) column1value
FROM my_table
If you want to permanently update the data in the table:
UPDATE my_table
SET column1value = CONCAT(
column1value,
column2value,
column3value,
column4value,
column5value
)
If you furthermore want to remove the old columns:
ALTER my_table
DROP column2value,
DROP column3value,
DROP column4value,
DROP column5value
you can do it like that
select concat(column1value,column2value,column3value,column4value,column5value)
AS allvalues from table1
little demo here
to be all in column 1
UPDATE my_table
SET column1 = CONCAT(column1value,column2value,column3value,column4value,column5value
)
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
Hi i have a mysql table without primary key and i need to delete the duplicated rows. how can i do so?
user_id category_id
1 2
1 3
1 4
1 2
2 2
2 3
2 2
CREATE TABLE temp SELECT DISTINCT * FROM tablename;
ALTER TABLE tablename RENAME junk;
ALTER TABLE temp RENAME tablename;
Since you cannot differentiate 2 identical rows, you cannot delete just one of them. The way you need to think about it is like this:
insert into new_better_table
select user_id, category_id from old_table group by user_id, category_id
It's possible to use a dirty flag with default value 1 and copy only single records with the flag set to 0, then simply remove all dirty records. This way you don't need another table.
Assuming you already created a dirty flag with default value 1:
insert into mytable
select fld1,fld2,fldN,0 as dirty
from mytable
group by duplicate_field
Then you can just delete dirty records:
delete from mytable where dirty = 1
Don't forget to remove the dirty flag. You're done.
The select distinct * solution proposed in another answer will not work in cases where your rows contain null values. A better solution that also prevents future duplicates from appearing in my_table in the first place is as follows:
create table no_dupes like my_table;
alter table no_dupes add unique index(unique_column,or_columns);
insert ignore into no_dupes select * from my_table;
rename table my_table to junk, no_dupes to my_table;
Is there a way to do an insert under a count condition, something like:
INSERT INTO my_table (colname) VALUES('foo') IF COUNT(my_table) < 1
Basically I want to insert a single default record if the table is currently empty. I'm using mysql.
Use SELECT instead of VALUES to be able to expand the query with a WHERE clause.
EXISTS is a better & faster test than COUNT
INSERT INTO my_table (colname)
SELECT 'foo'
WHERE NOT EXISTS (SELECT * FROM my_table)
One way would be to place a unique key on a column. Then execute a REPLACE:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
REPLACE works exactly like INSERT,
except that if an old row in the table
has the same value as a new row for a
PRIMARY KEY or a UNIQUE index, the old
row is deleted before the new row is
inserted
This is easier to read:
INSERT INTO my_table (colname)
SELECT 'foo' FROM DUAL
WHERE NOT EXISTS (SELECT * FROM my_table);
The lack of a VALUES is mitigated by the SELECT FROM DUAL which will provide the values. the FROM DUAL is not always required, but it doesn't hurt to include it for that weird configurations where it is required (like the installation of Percona I am using).
The NOT EXISTS is faster than doing a count which can be slow on a table with a large number of rows.