dump query on multiple table - mysql

Im trying to have a sql output file from a query between three tables.
Below is my code:
mysqldump --user=user --password=password --host=localhost dbname --where="SELECT v.ITEM_ID , v.CODE ,\
s.SENSOR , d.DESTINATIONCODE \
FROM V_TABLE v, S_TABLE s ,D_TABLE d \
WHERE s.ITEM_ID = v.ITEM_ID \
AND s.CREATIONDATETIME < '2014-2-16 00:00:00'\
AND v.DESTINATION_ID=d.ID" > /var/www/dumps/output_15_12_2017.sql
this give me an error:
mysqldump: Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM `COMMANDSPOOL` WHERE SELECT v.ITEM_ID , v.CODE ,s.SENSOR , d.DESTINATIONCODE FROM V_TABLE v, S_TABLE s ,D_TABLE d WHERE s.ITEM_ID = v.ITEM_ID AND s.CREATIONDATETIME < '2014-2-16 00:00:00' AND v.DESTINATION_ID=d.ID': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT v.ITEM_ID , v.CODE ,s.SENSOR , d.DESTINATIONCODE FROM V_TABLE' at line 1 (1064 )
maybe it is an error in using \

To use a query like that you could send it into the mysql client, (not the mysqldump utility) and then redirect that output to the file instead:
echo "SELECT v.ITEM_ID , v.CODE ,\
s.SENSOR , d.DESTINATIONCODE \
FROM V_TABLE v, S_TABLE s ,D_TABLE d \
WHERE s.ITEM_ID = v.ITEM_ID \
AND s.CREATIONDATETIME < '2014-2-16 00:00:00'\
AND v.DESTINATION_ID=d.ID\
" | mysql --user=user --password=password --host=localhost dbname > /var/www/dumps/output_15_12_2017.sql
If you're going this route then the --batch and --table options can be useful.

Related

Sphinxsearch: 1064 can not use HAVING with attribute not related to GROUP BY

I am using the example sphinxsearch config and the 'test' database that comes with the download for version 3.1.1
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306 # optional, default is 3306
sql_query = \
SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
FROM documents
sql_attr_uint = group_id
sql_attr_timestamp = date_added
}
When running this:
$array = $sphinx->prepare("SELECT date_added,group_id FROM test1 GROUP BY group_id HAVING group_id > 1");
$array->execute();
I get this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 can not use HAVING with attribute not related to GROUP BY
Use where condition instead of having since you didn't use any aggregation also group by is not necessary in this case
SELECT date_added,group_id
FROM test1
where group_id>1

How to take databse dump of ABC database of table XYZ and only of organization_id "22"?

Lets Say I have database named ABC.
And tables X,Y,Z,M,N which has organization_id 0-50.
How is it possible to create a dump file for tables X,Y,Z with only organization_id=22 ?
You could go through table by table and do
mysqldump -uroot -p db_name table_name --where='id=22'
or you can use
SELECT * INTO OUTFILE 'data_path.sql' from table where id=22
using mysqldump tool you can do that , as you mentioned your database is ABC , and tables are X,Y,Z.hope this works for you.
mysqldump -u db_user -p ABC X Y Z --no_create_info \
--lock-all-tables --where 'organization_id \
in (SELECT X.organization_id FROM X \
JOIN Y ON X.organization_id = Y.organization_id \
JOIN Z ON Y.organization_id = Z.organization_id \
WHERE X.organization_id = 22)' > data.sql

I am trying to run MYSQL command using SSH on the remote server. Getting the below error

I am trying to run MYSQL command using SSH on the remote server. Getting the below error
ssh -p 22 root#eseemon63 "mysql --compress --secure-auth --database nacoma --execute 'SELECT time,object_name FROM changelog_history WHERE object_type = 'host' AND oldname = 'New Host' AND time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY time;'"
Error is:
ERROR 1102 (42000): Incorrect database name 'Host AND time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY time;'
You will have to escape the extra single quotes like below using \ or using another '
ssh -p 22 root#eseemon63 "mysql --compress --secure-auth --database nacoma --execute 'SELECT time,object_name FROM changelog_history WHERE object_type = \'host\' AND oldname = \'New Host\' AND time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY time;'"
Its fixed by entering "'"
ssh -p 22 root#eseemon63 "mysql --compress --secure-auth --database nacoma --execute 'SELECT time,object_name FROM changelog_history WHERE object_type = 'host' AND oldname = 'New Host' AND time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY time;'"

ERROR at line 1: Unknown command '\ '

When I run the SQL script to export data out of MySQL database, using command line, I get the above error.
The SQL query works fine when run in phpMyAdmin, but just when run from command line throws an error.
Here is the command line I am using:
cat my_export | mysql -uxyzuser -pabcpassword mydb > export072911.txt
The code in my_export is as follows:
SELECT CONCAT( custfirstname, ' ', custlastname ) AS fullname, custcompany, \
SPACE( 10 ) AS custtitle, custaddressone, custaddresstwo, custcity, custstate, \
custzip, SPACE( 10 ) AS dummy, custphone, SPACE( 10 ) AS custfax, custemail, \
event_id, SPACE( 10 ) AS ticket1, SPACE( 10 ) AS ticket2, \
SPACE( 10 ) AS ticket3, SPACE( 10 ) AS ticket4, orderdate, b.quantity, \
FROM order_master a \
LEFT JOIN order_detail b ON b.order_master_id = a.id \
LEFT JOIN customer c ON c.email = a.custemail \
WHERE a.orderdate > '2010-12-01'\
AND a.event_id = '30' \
AND a.orderstatus = 'O' \
AND b.litype = 'ITEM' \
AND b.reftag = 'PKG' \
ORDER BY a.orderdate DESC;
You can safely delete all the backslashes and use input redirection rather than piping. The backslashes are needed if you are working with the SQL as a shell variable, but not for piping or redirection.
mysql -uxyzuser -pabcpassword mydb < my_export > export072911.txt
UPDATE After a quick test of my own, it looks like the pipe works just as well as input redirection as long as the backslashes are removed.

Declare variable in Bash?

I'm connected to my DB from the bash. I do a select count of an array and I want to stock the return in a variable. How can I do that?
I did:
var=`"select count(*) from shop_tab where catalog <> ''" | mysql -h abcdcef.com --port=3306 --user=root --password=hbbfe shop`
The request return a number but it doesn't stock into the variable.
Thanks!
EDIT: It works with this command line:
myvar = $(echo "select count(*) from shop_tab where catalog <> '';" | mysql -h abcdcef.com --port=3306 --user=root --password=hbbfe shop)
An easier way is :
var=$(mysql -h abcdcef.com --port=3306 --user=root --password=hbbfe --batch --skip-column-names -Dshop -e "select count(*) from shop_tab where catalog <> ''")
Moreover, I'll preconize the use of function in order to easily add options to the MySQL command without having to modifying all your script.
function MysqlQuery() {
mysql -h abcdcef.com --port=3306 --user=root --password=hbbfe --batch --skip-column-names -D "$1" -e "$2";
}
va=$(MysqlQuery Shop "SELECT COUNT(*) FROM shop_tab WHERE catalog <> ''")
vaABC=$(MysqlQuery Shop "SELECT COUNT(*) FROM shop_tab WHERE catalog <> 'abc'")
vadef=$(MysqlQuery Shop "SELECT COUNT(*) FROM shop_tab WHERE catalog <> 'def'")
# ...
I find this more readable too...
I think you are forgetting an echo in the pipe? Like this:
var=`echo "select count(*) from shop_tab where catalog <> ''" | mysql -h abcdcef.com --port=3306 --user=root --password=hbbfe shop`