?
Solved

MySQL: Query from within BASH script

Posted on 2011-09-09
2
Medium Priority
?
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1200 total points
ID: 36512120
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 800 total points
ID: 36513449
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

649 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