Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2012, 04:20 PM   PM User | #1
Vernk
Regular Coder

 
Join Date: Oct 2011
Posts: 113
Thanks: 9
Thanked 1 Time in 1 Post
Vernk is an unknown quantity at this point
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
Vernk is offline   Reply With Quote
Old 09-24-2012, 09:12 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-24-2012, 09:54 PM   PM User | #3
Vernk
Regular Coder

 
Join Date: Oct 2011
Posts: 113
Thanks: 9
Thanked 1 Time in 1 Post
Vernk is an unknown quantity at this point
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
Vernk is offline   Reply With Quote
Old 09-24-2012, 11:44 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-25-2012, 02:25 AM   PM User | #5
Vernk
Regular Coder

 
Join Date: Oct 2011
Posts: 113
Thanks: 9
Thanked 1 Time in 1 Post
Vernk is an unknown quantity at this point
Thanks, looked like it worked!
Vernk is offline   Reply With Quote
Old 10-09-2012, 06:09 AM   PM User | #6
JefferyJamison
New Coder

 
Join Date: Sep 2012
Location: USA
Posts: 13
Thanks: 0
Thanked 2 Times in 2 Posts
JefferyJamison is an unknown quantity at this point
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.
JefferyJamison is offline   Reply With Quote
Old 10-09-2012, 04:05 PM   PM User | #7
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
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.
guelphdad is offline   Reply With Quote
Old 10-09-2012, 08:23 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:55 PM.


Advertisement
Log in to turn off these ads.