Solved

MySQL/Linux: Multiple Queries from BASH

Posted on 2011-09-08
2
635 Views
Last Modified: 2012-06-27
Using BASH on Linux I want to run the following MySQL queries:
# Host: abc.example.com
# Username: ujm
# Password: p123
# Database: ujm_d

DELETE a FROM Group_Members a inner join Member b ON a.MemberID = b.ID  WHERE (a.GroupID = '27' OR a.GroupID = '26' OR a.GroupID = '25' OR a.GroupID = '24' OR a.GroupID = '22' ) AND b.UserSuspended = '1'
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '18', `ID` from `Member` WHERE `UserSuspended` = '1'
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '27', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` = '1' AND `PremWebMember` = '1' AND `SMSMember` = '1'
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '26', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` = '1' AND `PremWebMember` = '1' AND `SMSMember` != '1'
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '25', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` = '1'
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '24', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` != '1' AND `SMSMember` = '1'
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '22', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` != '1'

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 8

Accepted Solution

by:
pilson66 earned 250 total points
ID: 36503854
echo "DELETE a FROM Group_Members a inner join Member b ON a.MemberID = b.ID  WHERE (a.GroupID = '27' OR a.GroupID = '26' OR a.GroupID = '25' OR a.GroupID = '24' OR a.GroupID = '22' ) AND b.UserSuspended = '1'" | mysql -uujm -pp123 -Dujm_d

then the same principe for each query
0
 
LVL 38

Assisted Solution

by:wesly_chen
wesly_chen earned 250 total points
ID: 36504608
Usage:
mysql -u ujm -p p123 -h abc.example.com  -D ujm_d  -e "MYSQL QUERY ; "

mysql -u ujm -p p123 -h abc.example.com  -D ujm_d  -e "DELETE a FROM Group_Members a inner join Member b ON a.MemberID = b.ID  WHERE (a.GroupID = '27' OR a.GroupID = '26' OR a.GroupID = '25' OR a.GroupID = '24' OR a.GroupID = '22' ) AND b.UserSuspended = '1'; "

mysql -u ujm -p p123 -h abc.example.com  -D ujm_d  -e "INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '18', `ID` from `Member` WHERE `UserSuspended` = '1' ; "

or combine then into one with ";" to separate them

mysql -u ujm -p p123 -h abc.example.com  -D ujm_d  [b]-e[/b] "INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '27', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` = '1' AND `PremWebMember` = '1' AND `SMSMember` = '1'; 
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '26', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` = '1' AND `PremWebMember` = '1' AND `SMSMember` != '1'; 
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '25', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` = '1' ;
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '24', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` != '1' AND `SMSMember` = '1' ; 
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '22', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` != '1' ; "

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now