Solved

MySQL: Query from within BASH script

Posted on 2011-09-09
2
613 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
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 200 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get maximum transfer speed over LAN 4 84
VMware Workstation 12 Player 16 75
Run same command on multiple files in Linux 3 32
error log using ftp 7 41
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 …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

803 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