0

I need help with the following SQL

I have two tables titles and postcode_db I want to LEFT JOIN and INSERT into a third table

SELECT titles.id, titles.title, titles.address, 
postcode_db.postcode, postcode_db.suburb
FROM titles
LEFT JOIN postcode_db ON titles.address = postcode_db.suburb;

Above works. I want to insert into titles_postcode

titles.id AS titles_postcode.title_id

titles.title AS titles_postcode.title

postcode_db.postcode

INSERT INTO titles_postcode 
(titles.id AS titles_postcode.title_id, 
titles.title AS titles_postcode.title, 
postcode_db.postcode AS titles_postcode.postcode)
SELECT titles.id, titles.title, titles.address, 
postcode_db.postcode, postcode_db.suburb
FROM titles
LEFT JOIN postcode_db ON titles.address = postcode_db.suburb;

Its returning error right syntax to use near 'AS titles_postcode.title_id, titles.title AS titles_postcode.title, postcode_db.' at line 1

1

3 Answers 3

1

Try this:

INSERT INTO titles_postcode (title_id, title, postcode)
SELECT titles.id, titles.title, postcode_db.postcode
FROM titles
LEFT JOIN postcode_db ON titles.address = postcode_db.suburb;
1
  • Column doesnt match value count
    – Fabio
    Commented Feb 24, 2014 at 10:38
1

You shouldn't use alias in your insert query and also your values doesn't match column count, i asssume you only need those 3 columns to be inserted

INSERT INTO titles_postcode  (title_id, title, postcode)
SELECT titles.id, titles.title, postcode_db.postcode
FROM titles
LEFT JOIN postcode_db 
ON titles.address = postcode_db.suburb;
2
  • @KevinBowersox Not in a INSERT INTO ... SELECT... form. Commented Feb 24, 2014 at 10:41
  • @stephenmelb you are welcome man, if you feel my answer was correct and helped you out you might consider accepting it so you will also help people in the future with similar problems. To accept you just need to click on the large green tick mark (✔) under the answer's score
    – Fabio
    Commented Feb 24, 2014 at 10:43
1
INSERT INTO titles_postcode(title_id,title,postcode) 
SELECT titles.id, titles.title, titles.address, 
postcode_db.postcode
FROM titles
LEFT JOIN postcode_db ON titles.address = postcode_db.suburb;

Not the answer you're looking for? Browse other questions tagged or ask your own question.