Solved

move records one table to another

Posted on 2016-10-17
14
65 Views
Last Modified: 2016-10-20
I want to move my records inactive_skills to skills_admin. but records  not repeat (not move duplicate records)

table structure
CREATE TABLE `inactive_skills` (
  `id` int(11) NOT NULL,
  `skill` varchar(200) NOT NULL,
  `status` enum('0','1') NOT NULL,
  `created` datetime NOT NULL
) 

CREATE TABLE `skills_admin` (
  `id` int(11) NOT NULL,
  `skill` varchar(110) NOT NULL,
  `skill_info` varchar(255) NOT NULL,
  `created` datetime NOT NULL
)

Open in new window

0
Comment
Question by:Amita Singh
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41846520
Hi!

The easiest way is to do an insert like this.

This moves all data between tables
insert into skills_admin select distinct * from inactive_skills;

Open in new window


And if you want just subset of the distinct data you add a where clause to the select statement.
For example
insert into skills_admin select distinct * from inactive_skills where date(created) >= '2016-01-01';

Open in new window


Regards,
     Tomas Helgi
1
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 41846523
Maybe that ID in skills_admin is defined as primary key. What about table Indexes?
Could you show us the complete structure of skills_admin?
0
 
LVL 2

Author Comment

by:Amita Singh
ID: 41846536
please see the attachment.
skills_admin-1-.sql
inactive_skills-1-.sql
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 2

Author Comment

by:Amita Singh
ID: 41846548
Hi Tomas,
it's show error.
error.png
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 41846553
ALTER TABLE `skills_admin`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Open in new window

This means you cannot simply move your records by ID from inactive_skills to Skills_admin
also there's a
ADD UNIQUE KEY `no_duplicate` (`skill`) USING BTREE,

Open in new window

so your result in moving records is as expected: @(not move duplicate records)
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41846554
What duplicates are there in the data. You have a unique key on id and skill.
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41846634
Hi!

Hmmm, I didn't look close enough on your tables.  Since the tables are not identical then you can't use select * ... in the insert statement.
Also since id is auto incremented in both tables the you can skip that column in the insert like this

insert into skills_admin(skill,skill_info,created) select skill,'some info' as skill_info,created from inactive_skills; 

Open in new window


Note that since skill_info is not null then that column becomes mandatory (that is you have to put some data into it).
But as the inactive_skill table doesn't have any skill_info column my insert/select statement above puts a "default" value for every skill which you need to take a closer look at and change.
Every distinct skill value should have a 1:1 mapping to some skill_info value I presume. :)

Regards,
     Tomas Helgi
0
 
LVL 2

Author Comment

by:Amita Singh
ID: 41847992
when i run this sql its show error. and i need to add where clause. @ Tomas
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41849091
HI!

What error do you get ?
When I run my insert statement it completes successfully on the test data I made.
Also what where clause do you need to add ? It should be fairly simple for you to
add it to the statement.

Regards,
      Tomas Helgi
0
 
LVL 2

Author Comment

by:Amita Singh
ID: 41851453
#1062 - Duplicate entry 'PHP' for key 'no_duplicate'

and i want add where id = some id
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41851579
Hi!

Then you simply add distinct keyword to the select statement  like this

insert into skills_admin(skill,skill_info,created) select distinct skill,'some info' as skill_info,created from inactive_skills; 

Open in new window


and with a where clause on id. Note that I use a subselect to select a range of id's in my example.
You change the where clause to your needs.

insert into skills_admin(skill,skill_info,created) select distinct skill,'some info' as skill_info,created from inactive_skills where id in (select id from inactive_skills where id between 1 and 10 )

Open in new window


With where clause on just one id
insert into skills_admin(skill,skill_info,created) select distinct skill,'some info' as skill_info,created from inactive_skills where id = 10 

Open in new window


Regards,
    Tomas Helgi
0
 
LVL 2

Author Comment

by:Amita Singh
ID: 41851583
Thanks for reply Tomas,
i get error

9 errors were found during analysis.

    An expression was expected. (near "distinct" at position 58)
    Unrecognized keyword. (near "distinct" at position 58)
    Unexpected token. (near "skill" at position 67)
    Unexpected token. (near "," at position 72)
    Unexpected token. (near "'some info'" at position 73)
    Unrecognized keyword. (near "as" at position 85)
    Unexpected token. (near "skill_info" at position 88)
    Unexpected token. (near "," at position 98)
    Unexpected token. (near "created" at position 99)

Open in new window

0
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 41851666
Hi!

Select distinct should work with insert and this error is strange.
In what environment are you running the statement? Try to type the statement instead of copy/paste the statement and run it.

Try this

insert IGNORE into skills_admin(skill,skill_info,created) select  skill,'some info' as skill_info,created from inactive_skills where id in (select id from inactive_skills where id between 1 and 10 )

Open in new window


insert IGNOREinto skills_admin(skill,skill_info,created) select skill,'some info' as skill_info,created from inactive_skills where id = 10

Open in new window



The IGNORE keyword ignores rows that would cause duplicates and you would need to handle them in a different way.

http://dev.mysql.com/doc/refman/5.7/en/insert-select.html

Regards,
   Tomas Helgi
0
 
LVL 2

Author Closing Comment

by:Amita Singh
ID: 41851772
Thank You.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question