-1

I'm trying to insert values from two tables into a new table, but I keep getting the 'column count does not match' when it does. This is what I have so far:

create table contribution (
  contb_id      integer primary key auto_increment,
  cand_id       varchar(12),            
  contbr_id     varchar(12),            
  amount        numeric(8,2),           
  date          varchar(20),            
  election_type varchar(20),            
  tran_id       varchar(20),            
  foreign key (cand_id) references candidate,
  foreign key (contbr_id) references contributor
);

and to insert the values into it:

INSERT INTO contribution (cand_id, contbr_id, amount, date, election_type, tran_id)
SELECT (cand_id, cb.contbr_id, contb_receipt_amt, contb_receipt_dt, election_tp, tran_id) 
FROM campaign, contributor cb

Any help would be greatly appreciated!

Ethan
  • 1

1 Answers1

0

Essentially the answer is here: https://stackoverflow.com/a/5391390/12672179

For your case when you are inserting using select you should NOT have the brackets. Brackets in a select statement act very funky and essentially is making each column combine into 1 in its view. So to fix the column count issue in your INSERT statement just do this

  INSERT INTO contribution (cand_id, contbr_id, amount, date, election_type, tran_id)
      SELECT cand_id, cb.contbr_id, contb_receipt_amt, contb_receipt_dt, election_tp, tran_id 
  FROM campaign, contributor cb

Additionally this is still wrong as you cannot do two from tables in the same FROM instead you should JOIN the second table ON a value where the two tables match (In other words the foreign key field). I'm not going to tell you which type of JOIN to use as that depends on your data and if every single value has a link or not.

Finally you should also include a WHERE so sql knows which values to fetch if you only want to do specific and not all.

The Grand J
  • 348
  • 2
  • 5
  • 14