Solved

MySQL: INSERT INTO for each result

Posted on 2011-09-05
3
315 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 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Grouping 2 48
PHP: concatenate query 13 79
Restore of mysql database from .SQL file - using Coldfusion 5 41
two ways encryption with php 3 37
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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