0

I am trying to insert sap_id from butikkdata2 into energy_stage_ext2 using a left join on male_id.

This is my query:

insert into energi_stage_ext2  ( maale_id, maale_date, kwh_t, lev_id, 
                                 konsept, name, slag, sap_id )
select butikkdata2.sap_id 
  from butikkdata2 left join butikkdata2
          ON energi_stage_ext2.maale_id=butikkdata2.maale_id;

But I get the error message: Not unique table/alias.

Any ideas?

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
Alexander
  • 624
  • 2
  • 7
  • 18

1 Answers1

0

You're doing from butikkdata2 left join butikkdata2, and then you're asking energi_stage_ext2.maale_id=butikkdata2.maale_id.

How does it know which butikkdata2 you want? The one in FROM or the one in JOIN?

You need to give one (or both) an alias.

INSERT INTO energi_stage_ext2 (maale_id, maale_date, kwh_t, lev_id, konsept, name, slag, sap_id)
SELECT bu1.sap_id 
FROM butikkdata2 AS bu1
LEFT JOIN butikkdata2 AS bu2 ON energi_stage_ext2.maale_id=bu2.maale_id;

Now, I'm named them bu1 and bu2. Make sure that in your query you are refering the right field from the right table.

P.S. I'm not sure if you can actually do energi_stage_ext2.maale_id=bu2.maale_id. You aren't JOINing energi_stage_ext2 in your query. You might need to JOIN that first, I'm not 100% sure though.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • You are correct in assuming I am not able to do -´energi_stage_ext2.maale_id=bu2.maale_id´. That gives me unknown column in on clause. Hmm. I need to approach this in another way. Thanks. – Alexander Dec 04 '13 at 21:08
  • @user2718837: My suggestion is to just focus on the `SELECT` for now. Get that working, then add the `INSERT INTO`. P.S. The `SELECT` should return the same number of fields as you are `INSERT`ing. – gen_Eric Dec 04 '13 at 21:32