Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

move records one table to another

Posted on 2016-10-17
14
Medium Priority
?
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 23

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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 2

Author Comment

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

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 59

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this article, we’ll look at how to deploy ProxySQL.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

715 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