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;
Related
I have 2 tables : OSUSR_1SV_STAGING_FTP and #OSUSR_1SV_STAGING_FTP1
with same columns on both the table as :
Customer_Part_Number,
Lear_Part_Number,
Shipping_ID,
Customer_Name,
Effective_Date,
End_Date,Change_ID,
PO_Number,
PO_Price
I have successfully copied all the data to table #OSUSR_1SV_STAGING_FTP1 from table OSUSR_1SV_STAGING_FTP.
But my scenario stucks when the records are same in table OSUSR_1SV_STAGING_FTP and I don't want it to get it inserted to table #OSUSR_1SV_STAGING_FTP1.
Just need to insert non duplicate records.
This inserts records only which are in OSUSR_1SV_STAGING_FTP but not in OSUSR_1SV_STAGING_FTP1
INSERT INTO OSUSR_1SV_STAGING_FTP1
SELECT * FROM OSUSR_1SV_STAGING_FTP
EXCEPT
SELECT * FROM OSUSR_1SV_STAGING_FTP1
First off you need to have a unique id for each row. Then you can select from Table 1 (OSUSR_1SV_STAGING_FTP) and insert into Table 2 (#OSUSR_1SV_STAGING_FTP1) where it doesn't already exist.
INSERT INTO #OSUSR_1SV_STAGING_FTP1
SELECT *
FROM OSUSR_1SV_STAGING_FTP
WHERE OSUSR_1SV_STAGING_FTP.[Unique_ID] NOT IN
(SELECT Unique_ID FROM #OSUSR_1SV_STAGING_FTP1)
Noel
Given the following table structure:
ID
Field1
Field2
Field3
I would like to duplicate a record in the table. The pseudo code would look something like this:
DUPLICATE * IN MyTable WHERE ID = 3
Here are my constraints:
I don't want the ID duplicated.
I do not now how many fields or their types are in the table.
I do not want to do this in PHP.
You can use a select statement to insert rows. If you want to insert a new row with all of the same values, just write a select statement to get the values you want. Try something like this:
INSERT INTO myTable (field1, field2, field3)
SELECT field1, field2, field3 FROM myTable WHERE id = 3;
Here is an SQL Fiddle example.
EDIT
If you don't know how many fields, an option you have is to use a temporary table. See this article for example. The reason you need a temporary table, is because you cannot simply re-insert the row, because you will have a duplicate key error. So the logic will be as follows: Add row to new table, change the id, insert duplicated row with new id into old table, and drop the temporary table.
Assuming your id column is auto_increment, you can set the new id to the current maximum id + 1. You don't have to do that, but as long as you have some way of updating the id to something that does not already exist, this will work. Try this instead:
CREATE TEMPORARY TABLE temp SELECT * FROM myTable WHERE id = 3;
UPDATE temp SET id = (SELECT MAX(id) FROM myTable) + 1;
INSERT INTO myTable SELECT * FROM temp;
DROP TABLE temp;
Here is another SQL Fiddle example.
Hi Here i came across a situation in which by mistakenly Without dropping the table i have run the batch file of the table which consists of some insert statements in detail
I have a table like alert_priority consists of records like
Id priority_name
--- --------------
1 P0
2 P1
3 P2
and now by mistakenly without dropping alert_priority i have executed script file of the table which consists of some insert statements and now after executing the script my records in the table are like
Id priority_name
--- --------------
1 P0
2 P1
3 P2
1 P0
2 P1
3 P2
Now i want to delete the records which are extra(records after Id 3) and i should have all the records which are present before i have executed the script file.
Although i have an option to drop the table and execute the script file once again, I wanted to know is there any way which we can do through sql query
I have no primay keys in the table
First , consider setting your ID fields as AI (auto increasment) and even PK (Primary Key).
In order to remove those duplicated rows , we will create a new table and will move all
those duplicated rows to it.
After that , drop that table.
CREATE TEMPORARY TABLE bad_temp AS SELECT DISTINCT * FROM alert_priority
you can copy all unique records into a new table, then delete the old table:
SELECT DISTINCT * INTO new_table FROM old_table
In SQL-Server it would be easy using ROW_NUMBER, but alas MySQL doesn't have a function like that :-(
Best way to solve it would be as follows:
Create a new table identical in structure to the first, but with no
data.
Use the query: INSERT INTO name_of_new_table SELECT DISTINCT * FROM name_of_old_table
Drop the old table
Rename the new table to whatever the old table was called.
CREATE TABLE new_tbl(id int AUTO_INCREMENT,priority_name);
INSERT INTO new_tbl
select priority_name from old_tbl group by priority_name;
To just delete the duplicate new rows and leave the old ones in place (on the basis that I assume there are already other tables whose rows refer to the original rows):-
DELETE FROM alert_priority
WHERE Id IN (SELECT MaxId
FROM (SELECT priority_name, MAX(Id) AS MaxId, COUNT(Id) AS CountId
FROM alert_priority
GROUP BY priority_name
HAVING CountId > 1))
Following query will give you all records that you want to keep:
SELECT min(id)
FROM alert_priority
GROUP BY priority_name
HAVING count(*) > 1
OR min(id) = max(id)
To remove all duplicates, run this query:
DELETE FROM alert_priority
WHERE id NOT IN (
SELECT min(id)
FROM alert_priority
GROUP BY priority_name
HAVING count(*) > 1
OR min(id) = max(id)
)
Hi Here i came across a situation in which by mistakenly Without dropping the table i have run the batch file of the table which consists of some insert statements in detail
I have a table like alert_priority consists of records like
Id priority_name
--- --------------
1 P0
2 P1
3 P2
and now by mistakenly without dropping alert_priority i have executed script file of the table which consists of some insert statements and now after executing the script my records in the table are like
Id priority_name
--- --------------
1 P0
2 P1
3 P2
1 P0
2 P1
3 P2
Now i want to delete the records which are extra(records after Id 3) and i should have all the records which are present before i have executed the script file.
Although i have an option to drop the table and execute the script file once again, I wanted to know is there any way which we can do through sql query
I have no primay keys in the table
First , consider setting your ID fields as AI (auto increasment) and even PK (Primary Key).
In order to remove those duplicated rows , we will create a new table and will move all
those duplicated rows to it.
After that , drop that table.
CREATE TEMPORARY TABLE bad_temp AS SELECT DISTINCT * FROM alert_priority
you can copy all unique records into a new table, then delete the old table:
SELECT DISTINCT * INTO new_table FROM old_table
In SQL-Server it would be easy using ROW_NUMBER, but alas MySQL doesn't have a function like that :-(
Best way to solve it would be as follows:
Create a new table identical in structure to the first, but with no
data.
Use the query: INSERT INTO name_of_new_table SELECT DISTINCT * FROM name_of_old_table
Drop the old table
Rename the new table to whatever the old table was called.
CREATE TABLE new_tbl(id int AUTO_INCREMENT,priority_name);
INSERT INTO new_tbl
select priority_name from old_tbl group by priority_name;
To just delete the duplicate new rows and leave the old ones in place (on the basis that I assume there are already other tables whose rows refer to the original rows):-
DELETE FROM alert_priority
WHERE Id IN (SELECT MaxId
FROM (SELECT priority_name, MAX(Id) AS MaxId, COUNT(Id) AS CountId
FROM alert_priority
GROUP BY priority_name
HAVING CountId > 1))
Following query will give you all records that you want to keep:
SELECT min(id)
FROM alert_priority
GROUP BY priority_name
HAVING count(*) > 1
OR min(id) = max(id)
To remove all duplicates, run this query:
DELETE FROM alert_priority
WHERE id NOT IN (
SELECT min(id)
FROM alert_priority
GROUP BY priority_name
HAVING count(*) > 1
OR min(id) = max(id)
)
I want to create a new table with properties of an old table and without duplicates. I want to do something like this:
CREATE TABLE New_Users LIKE Old_Users,
AS
(SELECT * FROM Old_Users GROUP BY ID) ;
But the above is not working. Can anybody modify it to work?
Your attempt wasn't that bad. You have to do it with LIKE, yes.
In the manual it says:
Use LIKE to create an empty table based on the definition of another
table, including any column attributes and indexes defined in the
original table.
So you do:
CREATE TABLE New_Users LIKE Old_Users;
Then you insert with
INSERT INTO New_Users SELECT * FROM Old_Users GROUP BY ID;
But you can not do it in one statement.
Based on http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html
What about:
Create Table New_Users Select * from Old_Users Where 1=2;
and if that doesn't work, just select a row and truncate after creation:
Create table New_Users select * from Old_Users Limit 1;
Truncate Table New_Users;
EDIT:
I noticed your comment below about needing indexes, etc. Try:
show create table old_users;
#copy the output ddl statement into a text editor and change the table name to new_users
#run the new query
insert into new_users(id,name...) select id,name,... form old_users group by id;
That should do it. It appears that you are doing this to get rid of duplicates? In which case you may want to put a unique index on id. if it's a primary key, this should already be in place. You can either:
#make primary key
alter table new_users add primary key (id);
#make unique
create unique index idx_new_users_id_uniq on new_users (id);
For MySQL, you can do it like this:
CREATE TABLE New_Users SELECT * FROM Old_Users group by ID;
CREATE TABLE new_table LIKE old_table;
or u can use this
CREATE TABLE new_table as
SELECT * FROM old_table WHERE 1 GROUP BY [column to remove duplicates by];
You can use below query to create table and insert distinct values into this table:
Select Distinct Column1, Column2, Column3 into New_Users from Old_Users