Solved

MySQL: Query from within BASH script

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix process listing into CSV format 3 53
rsyslog raw message 4 38
SSH in linux 9 71
lftp to sync folders but do not keep files older than 30 days old on synced machine. 2 51
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…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

730 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