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

Posted on 2009-02-22
Last Modified: 2013-12-26
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?
Question by:GuruGary
    LVL 142

    Accepted Solution

    you can do it like this:
    "SELECT FROM data_bases db JOIN domains d ON db.dom_id= where'$SITE'"

    Open in new window

    LVL 10

    Author Closing Comment

    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")

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
    I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now