ChathurawinD
ChathurawinD

Reputation: 784

Insert selected multiple records to a table from existing table with another value

My first table is like this

Voter

userID  | lgDiv 
--------------------
   1     |   3
   2     |   4
   3     |   6
   4     |   3
   5     |   3

In my second table, both userID and elecID are primary keys

VoterElection

voterID | elecID | voterType| votingStatus
--------------
   1      |  1   | Normal  | Active
   2      |  1   | Normal  | Active
   3      |  3   | Normal  | Active

I want to insert some selected rows of Voter table to the VoterElection table with electionID, voterType and votingStatus. electionID, voterType and votingStatus are same values for all the selected items from the Votertable.

userID of Voter table will be voterID of VoterElection table.

Furthermore Assume that I select all users whose lgDiv is 3 like this

        SELECT userID FROM Voter WHERE lgDiv=3 ;

According to the data I have given here it will select 3 records.

I want to insert all three records I selected from Voter to VoterElection with elecID, voterType and votingStatus. For all the records elecID, voterType and voting status will be the same.

Assume that I select elecID =3 voterType='Normal' and votingStatus='Active' to all three records. After insert it should look like this.

VoterElection

voterID | elecID | voterType | votingStatus
--------------------------------------------
   1    |  1     | Normal    | Active
   4    |  1     | Normal    | Active
   5    |  1     | Normal    | Active

This is what I could grab by searching

INSERT INTO VoterElection (voterID)
   SELECT userID  
   FROM Voter 
   WHERE lgDiv = 3;

How can I include elecId, voterType and votingStatus ?

Upvotes: 0

Views: 66

Answers (1)

Peter
Peter

Reputation: 27934

You can just add the values in your SELECT INTO:

INSERT INTO VoterElection (voterID, elecID, voterType, votingStatus)
     SELECT userID, 1, 'Normal', 'Active' FROM Voter WHERE lgDiv=3;

You can change the values 1, 'Normal', 'Active' to whatever you want.

Upvotes: 2

Related Questions