Reputation: 11
I am trying to use following mysql code to insert on column whose name is decided on some value. It gives me error that "some_percent" unknown column 'some_percent' in 'field list'
set @SplitState = `some_percent`;
SET @ID = uuid();
INSERT INTO `rspca_donations`(`id`, `@SplitState`) values (@ID, '100');
Can someone please suggest correct way?
Upvotes: 0
Views: 693
Reputation: 521914
You can't use a user variable to represent a column name in this way. One option here would be to use a prepared statement in MySQL:
SET @SplitState = 'some_percent';
SET @ID = UUID();
SET @sql = CONCAT('INSERT INTO rspca_donations (id, ', @SplitState, ') VALUES (?, 100)');
PREPARE stmt FROM @sql;
EXECUTE stmt USING @ID;
Upvotes: 1