简体   繁体   English

Cron Bash-从两个MySQL表中选择数据并导出到CSV

[英]Cron Bash - select data from two MySQL tables & export to CSV

I am really bad at creating MySQL queries and need some help. 我真的很难创建MySQL查询,需要一些帮助。 I need to create a bash file to be triggered by a cron job once a week - that queries two tables, grabbing data where the user IDs match in both tables, and adding the select data to a CSV export file. 我需要创建一个由cron作业每周触发一次的bash文件-查询两个表,获取两个表中用户ID匹配的数据,并将选择数据添加到CSV导出文件中。 I would like the CSV to be comma separated. 我希望CSV以逗号分隔。 Right now the best I can get it tab separated. 现在最好的办法是将它分开。

My issue in getting this query to run is my syntax (which I know is wrong as I have simply stolen snippets from various articles online). 使此查询运行的问题是我的语法(我知道这是错误的,因为我只是从网上的各种文章中窃取了摘要)。 I did get each DB query to work separately (grabbing from one table with one query and another table with another query). 我确实让每个数据库查询都可以单独工作(从一个表与一个查询抓取,而从另一个表与另一个查询抓取)。 Now I need to combine them to grab only the data I need. 现在,我需要组合它们以仅获取我需要的数据。

Here's my current (non working) query: 这是我当前(无效)的查询:

#!/bin/bash
mysql -u USERNAME --password=PASSWORD --database=xxxx_DBNAME --execute='SELECT `xxxx_videotraining_user.user_id`, `xxxx_videotraining_user.training_title`, `xxxx_videotraining_user.status`, `xxxx_users.id`, `xxxx_users.name`, `xxxx_users.user_employer`, `xxxx_users.user_ss_number` WHERE `xxxx_videotraining_user.user_id` = `xxxx_users.id` AND `xxxx_videotraining_user.status` = "Completed" AND `xxxx_users.user_ss_number` > "1" ORDER BY `xxxx_videotraining_user.user_id` LIMIT 0, 10000 AND ' -C > /home/xxxx/subs/vtc/DB_EXPORTS/xxxx_videotraining_completed.csv

I think you can see what I am trying to accomplish here - any help would be greatly appreciated! 我想您可以在这里看到我要完成的工作-任何帮助将不胜感激!

我认为AND不应该在这里:

LIMIT 0, 10000 AND

It also looks like you're missing your FROM clause, have an trailing AND clause (as noted in other answers), and are quoting things incorrectly. 看起来您好像缺少FROM子句,尾随AND子句(如其他答案中所述),并且引用不正确。 This looks to be your original query: 这似乎是您的原始查询:

SELECT `xxxx_videotraining_user.user_id`, 
       `xxxx_videotraining_user.training_title`, 
       `xxxx_videotraining_user.status`, 
       `xxxx_users.id`, 
       `xxxx_users.name`, 
       `xxxx_users.user_employer`, 
       `xxxx_users.user_ss_number` 
 WHERE `xxxx_videotraining_user.user_id` = `xxxx_users.id` AND 
       `xxxx_videotraining_user.status` = "Completed" AND 
       `xxxx_users.user_ss_number` > "1" 
ORDER BY `xxxx_videotraining_user.user_id` 
LIMIT 0, 10000 AND 

I think you want to add the FROM clause, quote the table and field separately, and remove the trailing AND, to get something like: 我认为您想添加FROM子句,分别引用表和字段,并删除结尾的AND,以得到如下结果:

    SELECT `xxxx_videotraining_user`.`user_id`, 
       `xxxx_videotraining_user`.`training_title`, 
       `xxxx_videotraining_user`.`status`, 
       `xxxx_users`.`id`, 
       `xxxx_users`.`name`, 
       `xxxx_users`.`user_employer`, 
       `xxxx_users`.`user_ss_number` 
  FROM `xxxx_users`,
       `xxxx_videotraining_user`
 WHERE `xxxx_videotraining_user`.`user_id` = `xxxx_users`.`id` AND 
       `xxxx_videotraining_user`.`status` = "Completed" AND 
       `xxxx_users`.`user_ss_number` > "1" 
ORDER BY `xxxx_videotraining_user`.`user_id` 
LIMIT 0, 10000

There are other things that could be done to shorten the size of the query and make it a bit cleaner, but that should get it functional. 还可以做其他事情来缩短查询的大小并使它更整洁,但这应该可以使它起作用。

One thing I know that helps me when dealing with long queries is to format them like this, with the main clauses separated out so you can see the different sections of the query. 我知道在处理长查询时对我有帮助的一件事是这样格式化它们,将主要子句分隔开,以便您可以看到查询的不同部分。

Let me know if that helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM