Skip to content
All writing Part 08 of 09 · Reaching a Remote DB From Your Laptop
Engineering · 1 min read

The MySQL CLI: Four Ways to Feed Inline SQL

Four ways to feed SQL to mysql without an interactive session: a single -e statement, semicolon-separated statements, a quoted heredoc, or a .sql file; plus --batch for piping.

Four ways to feed SQL to mysql without an interactive session:

1. Single inline statement (-e):

mysql -e "SELECT * FROM asset WHERE uploaded_package_id = 100"

2. Multiple statements: semicolon-separate inside one -e:

mysql -e "USE app_production; SELECT COUNT(*) FROM asset; SELECT NOW();"

3. Heredoc: cleanest for multi-line; quote the delimiter ('SQL') so the shell won’t expand $ / backticks:

mysql -D app_production <<'SQL'
SELECT a.asset_id, a.`size`
FROM   asset AS a
WHERE  uploaded_package_id = 100;
SQL

4. From a .sql file:

mysql app_production < query.sql      # shell redirect
mysql -e "source query.sql"           # MySQL's own source command
  • For machine-parseable output: add --batch (tab-separated) + --skip-column-names (drop the header), then pipe to jq / Python.

From this session: piped --batch --skip-column-names output into Python to build a per-asset_id JSON config from the prod query result.

One statement → -e; multi-line → heredoc (quote the delimiter); piping → add --batch --skip-column-names.


References

Related: back to the remote DB overview; pairs with one-shot vs REPL and the MySQL connection flags.

Tags #mysql #databases #cli
// connect

Be brave | Be wise | Be grateful

21 BreakinCode

// elsewhere
LinkedInMedium (lang: en)Life RecordYoutube
wh:~$William Hung· © 2026 Taipei · GMT+8 · Available for collaboration