Solved

move records one table to another

Posted on 2016-10-17
14
51 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 24

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

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 24

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 24

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 24

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 24

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

758 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now