Solved

MySQL: Query from within BASH script

Posted on 2011-09-09
2
607 Views
Last Modified: 2012-05-12
This MySQL query is successfully executed from a MySQL command line:
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '22', `ID` from `Member` m WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` != '1' AND NOT EXISTS( select null from Group_Members g WHERE g.GroupID = 22 and g.MemberID = m.ID )

Open in new window


However when I try to put it in a BASH script like this, I get an error:
echo "INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '22', `ID` from `Member` m WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` != '1' AND NOT EXISTS( select null from Group_Members g WHERE g.GroupID = 22 and g.MemberID = m.ID )" | mysql -u xyz -pabc123 -h m.example.com -D xyz_db

Open in new window



Group_Members: Command not found.
GroupID: Command not found.
MemberID: Command not found.
ID: Command not found.
Member: Command not found.
UserSuspended: Command not found.
PremPlusMember: Command not found.
PremWebMember: Command not found.
SMSMember: Command not found.
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax;
0
Comment
Question by:hankknight
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
Comment Utility
you don't run it from "bash" itself, but using mysql command line tool:
http://dev.mysql.com/doc/refman/5.5/en/mysql.html
mysql db_name < script.sql > output.tab

Open in new window

0
 
LVL 1

Assisted Solution

by:spock787
spock787 earned 200 total points
Comment Utility
Assuming the command and SQL itself is correct, the backticks need to be excaped... try this...

echo "INSERT INTO \`Group_Members\` (\`GroupID\`,\`MemberID\`) SELECT '22', \`ID\` from \`Member\` m WHERE \`UserSuspended\` != '1' AND \`PremPlusMember\` != '1' AND \`PremWebMember\` = '1' AND \`SMSMember\` != '1' AND NOT EXISTS( select null from Group_Members g WHERE g.GroupID = 22 and g.MemberID = m.ID )" | mysql -u xyz -pabc123 -h m.example.com -D xyz_db

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

728 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

15 Experts available now in Live!

Get 1:1 Help Now