1

I have an issue, I want to take the value of the field 'coName' in my counties table and insert it into the 'catName' field of my categories table.

Problem is, there's 178 rows in the counties table so the following SQL doesn't quite work:

INSERT INTO categories
SET catName=
(
    SELECT coName
    FROM counties 
    WHERE coCountryId=201
)

Anybody know a way I can run this query 178 times and not duplicate the entries inserted.

1
  • What RDBMS are you using? Commented May 12, 2014 at 15:21

2 Answers 2

3

You can do it like this

INSERT INTO categories (coname)
    SELECT DISTINCT coName
    FROM counties 

Unless you are trying to do an update, that would be different. One important thing to note is: When you do INSERT INTO and SELECT you have to make sure you explicitly state each column and there needs to be a matching number of columns in the SELECT and the INSERT. So in this case I assumed your field in categories was coname...

Sign up to request clarification or add additional context in comments.

1 Comment

This has worked, thanks, I'll accept answer when it lets me in 10 minutes.
2
INSERT INTO categories
(catName)
(SELECT DISTINCT coName FROM counties)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.