Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post

    INSERT Rows where userid=(SELECT id FROM userinfo)

    Hello I'm trying to do something like this
    Code:
    INSERT INTO VotingSite (VotingSiteType, active) VALUES ('10', 0) WHERE userid=(SELECT ID FROM userinfo)
    Doesn't work any ideas?

    It's suppose to add a row with the userid

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,720
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    You can't use WHERE in INSERT.

    Period.

    Show us what fields you have in the VotingSite table.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post
    http://gyazo.com/9422820f30a3f8c7c6f03d04c1e92f26

    Each user has 11 rows to themselves which is are sites. But when we add a new site into the program we need to add a new row for every user

    ID USERID VOTINGSITETYPE ACTIVE

    How can I dynamically add a new row for every user in Users table

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,720
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    Ahhh...got it.

    Code:
    INSERT INTO VotingSite (userid, VotingSiteType, active) 
    SELECT ID, 10, 0 FROM userinfo
    See that? You can SELECT both fields *AND CONSTANT VALUES* from a table, so combine that with INSERT INTO and you are done.

    NOTE: Don't put apostrophes around numeric values ('10' in your example) unless the data type in question really is a string type.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Oct 2011
    Posts
    113
    Thanks
    9
    Thanked 1 Time in 1 Post
    Thanks, looked like it worked!

  • #6
    New Coder
    Join Date
    Sep 2012
    Location
    USA
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Insert Rows where userid=(select id from userinfo)

    Hi ! INSERT INTO VotingSite (userid, VotingSiteType, active)
    SELECT UID, 30, 1 FROM userinfo

    I think this will work.

  • #7
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Quote Originally Posted by JefferyJamison View Post
    Hi ! INSERT INTO VotingSite (userid, VotingSiteType, active)
    SELECT UID, 30, 1 FROM userinfo

    I think this will work.
    Yes that will work. If you read the thread closely, you'll see that solution was given and verified two weeks ago though.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,720
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    Quote Originally Posted by JefferyJamison View Post
    Hi ! INSERT INTO VotingSite (userid, VotingSiteType, active)
    SELECT UID, 30, 1 FROM userinfo

    I think this will work.
    Actually, no it won't work. If you look carefully at the first post, the field name in the userinfo table is ID

    No idea where you came up with UID but it's plain flat wrong.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •