Andy D'Arata Andy D'Arata - 1 month ago 18
MySQL Question

Bash scripting - Complex MYSQL statements

So I am trying to take this:

SELECT 'cran_juniper' AS `set`, c.*
FROM cran_juniper c
WHERE ROW(c.device_fqdn, c.device_ip, c.interface, c.admin_state, c.link_state, c.description) NOT IN
(
SELECT device_fqdn, device_ip, interface, admin_state, link_state, description
FROM cran_juniper_baseline
)
UNION ALL
SELECT 'cran_juniper_baseline' AS `set`, b.*
FROM cran_juniper_baseline b
WHERE ROW(b.device_fqdn, b.device_ip, b.interface, b.admin_state, b.link_state, b.description) NOT IN
(
SELECT device_fqdn, device_ip, interface, admin_state, link_state, description
FROM cran_juniper
)
into outfile 'today.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
;


And insert it into a bash script. Considering there's a SLEW of special characters all over this query, I am having a hard time finding a way to go about this. Any assist would be magical.

Answer

1st solution using here-doc :

mysql -h 127.0.0.1 <<'EOF'
SELECT  'cran_juniper' AS `set`, c.*
FROM    cran_juniper c
...
;
EOF

2nd solution using a separate file

mysql -h 127.0.0.1 < file.sql # you will put all of your query within this file

Check

man bash | less +/here-doc
Comments