Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MySQL: INSERT INTO for each result

Posted on 2011-09-05
3
Medium Priority
?
321 Views
Last Modified: 2012-05-12
This MySQL query returns 900 results:
SELECT `ID` from `Member` WHERE `UserSuspended` = '1'

Open in new window


For each of those 900 results, I want to do this:
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) VALUES ('18', '###  ID from the previous query ###')

Open in new window

0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 36484616
run this as one statement:

INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '18', `ID` from `Member` WHERE `UserSuspended` = '1'

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 36484997
angelIII, that query does not work.  The problem is it is that "Select '18'" makes it return all results.



## Returns 100000 Results
SELECT '18', `ID` from `Member` WHERE `UserSuspended` = '1'

## Returns 900 Results
SELECT * from `Member` WHERE `UserSuspended` = '1'
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36485029
adding another field into the query will not result in more or less records.
you must confuse things ...
difficult will be now to "see" what goes wrong in your case, I know for sure that INSERT ... SELECT ... works fine as such...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month20 days, 22 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question