Skip to content
Advertisement

MYSQL: Insert with Select not working properly

I can’t seem to debug this issue. I am currently running to set person_id to -1, but also utilize a select statement with this person_id value.

Using this statement, I cannot find any reason online or anywhere on why this does not function:

Insert into Person(person_id,firstName,middleName,lastName) 
Values(-1,(Select Distinct B.firstName, B.middleName, B.lastName 
           from Ballot B 
           where B.voter_Id = 0));

Result is: ERROR 1136 (21S01): Column count doesn’t match value count at row 1

Logically, this should function. But, I am unable to find out why. After all, the column counts definitely do match..

Any suggestions?

Advertisement

Answer

You seem to be looking for the insert ... select syntax:

Insert into Person(person_id, firstName, middleName, lastName) 
Select Distinct -1, firstName, middleName, lastName 
from Ballot B 
where voter_Id = 0

Demo on DB Fiddle (courtesy of VBokšić).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement