Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy don't my bash script create a mysql temporary table?
    primarykey
    data
    text
    <p>I'm stumped why I cannot make the temp table in the bash script.</p> <p>This works on the command prompt.</p> <pre><code>mysql&gt; CREATE TEMPORARY TABLE snorby.delete_me ( SELECT cid AS del_cid FROM snorby.event WHERE timestamp &lt; DATE_SUB(NOW(), INTERVAL 21 DAY) ); Query OK, 72 rows affected (0.08 sec) Records: 72 Duplicates: 0 Warnings: 0 mysql&gt; select * from snorby.delete_me limit 2; +---------+ | del_cid | +---------+ | 4 | | 5 | +---------+ 2 rows in set (0.00 sec) mysql&gt; DROP TABLE snorby.delete_me; Query OK, 0 rows affected (0.08 sec) mysql&gt; </code></pre> <p>But when I do that from the command line, intending to use it in the bash script, it does not make a table. Note: this root user has no password.</p> <pre><code>~$ sudo mysql -uroot -e "CREATE TEMPORARY TABLE snorby.delete_me (SELECT cid AS del_cid FROM snorby.event WHERE timestamp &lt; DATE_SUB(NOW(), INTERVAL 21 DAY) );" ~$ sudo mysql -uroot -e "SELECT * FROM snorby.delete_me LIMIT 2;" ERROR 1146 (42S02) at line 1: Table 'snorby.delete_me' doesn't exist </code></pre> <p>Lastly, if I put those commands in a .sql file and run them it also works fine. Example:</p> <pre><code>~$ mysql -uroot -e "SET @daystokeep=21; source snorby_del_old_by_days.sql;" </code></pre> <p>Thanks to barmar, I understand now. To run the whole thing from the command, it must be all one command, if using the temp table. Ex:</p> <pre><code>~$ sudo mysql -uroot -e "CREATE TEMPORARY TABLE snorby.delete_me (SELECT cid AS del_cid FROM snorby.event WHERE timestamp &lt; DATE_SUB(NOW(), INTERVAL 21 DAY) ); SELECT * FROM snorby.delete_me LIMIT 2; DROP TABLE snorby.delete_me;" </code></pre> <p>Or use a regular table.</p> <p>I thanks for the help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload