• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 111
  • Last Modified:

move records one table to another

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
Amita Singh
Asked:
Amita Singh
  • 6
  • 5
  • 2
  • +1
1 Solution
 
Tomas Helgi JohannssonCommented:
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
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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
 
Amita SinghWeb DeveloperAuthor Commented:
please see the attachment.
skills_admin-1-.sql
inactive_skills-1-.sql
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Amita SinghWeb DeveloperAuthor Commented:
Hi Tomas,
it's show error.
error.png
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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
 
Julian HansenCommented:
What duplicates are there in the data. You have a unique key on id and skill.
0
 
Tomas Helgi JohannssonCommented:
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
 
Amita SinghWeb DeveloperAuthor Commented:
when i run this sql its show error. and i need to add where clause. @ Tomas
0
 
Tomas Helgi JohannssonCommented:
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
 
Amita SinghWeb DeveloperAuthor Commented:
#1062 - Duplicate entry 'PHP' for key 'no_duplicate'

and i want add where id = some id
0
 
Tomas Helgi JohannssonCommented:
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
 
Amita SinghWeb DeveloperAuthor Commented:
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
 
Tomas Helgi JohannssonCommented:
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
 
Amita SinghWeb DeveloperAuthor Commented:
Thank You.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now