CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   INSERT Rows where userid=(SELECT id FROM userinfo) (http://www.codingforums.com/showthread.php?t=274031)

Vernk 09-24-2012 04:20 PM

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

Old Pedant 09-24-2012 09:12 PM

You can't use WHERE in INSERT.

Period.

Show us what fields you have in the VotingSite table.

Vernk 09-24-2012 09:54 PM

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

Old Pedant 09-24-2012 11:44 PM

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.

Vernk 09-25-2012 02:25 AM

Thanks, looked like it worked!

JefferyJamison 10-09-2012 06:09 AM

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.:)

guelphdad 10-09-2012 04:05 PM

Quote:

Originally Posted by JefferyJamison (Post 1277854)
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.

Old Pedant 10-09-2012 08:23 PM

Quote:

Originally Posted by JefferyJamison (Post 1277854)
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.


All times are GMT +1. The time now is 12:50 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.