Can you use the result of a SQL query inside another SQL query in the same statement?

I am writing a bash script to backup some information. I am currently a MySQL novice, but I figured out that I can find location(s) of this information by doing 2 SQL queries like:
"SELECT id FROM domains where name='$SITE' limit 1"
I then take the result of that ($DOMID) and use it in the other query like:
"SELECT name FROM data_bases where dom_id=$DOMID"

My question is can I combine the 2 queries to basically do something like:
"SELECT name FROM data_bases where dom_id=(SELECT id FROM domains where name='$SITE' limit 1)"

I am calling these from a bash script like:
mysql -usuer -ppassword -Ddatabase -e "SELECT ..."
So a related question would be, is there also a way to suppress the column labels and/or ASCII boxes around the output, so my parsing of the output would be easier?
LVL 10
GuruGaryAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
you can do it like this:
"SELECT db.name FROM data_bases db JOIN domains d ON db.dom_id= d.id where d.name='$SITE'"

Open in new window

0
 
GuruGaryAuthor Commented:
That works great, thanks!  And I figured out how to suppress the column headers (mysql switch "--skip-column-names") and the ASCII boxes (mysql switch "-s")
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.