How to feed mysql queries from bash

后端 未结 7 1189
小鲜肉
小鲜肉 2020-12-05 17:52

I\'m trying to make a bash script that creates a mysql user and database but I can\'t find a way to feed the sql into mysql, I\'m trying with this format:

my         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 18:03

    For big queries in a bash script, you can try:

    read -d '' SQL_QUERY_1 << EOF
    
    SELECT prod.id as id, prod.title as title, comp.name as company, pho.id as photo_id, pho.image as photo_name
    FROM products as prod
    JOIN accounts as comp
    ON comp.id = prod.account_id
    JOIN photos as pho
    ON pho.id = prod.featured_photo_id;
    
    EOF
    
    echo ${SQL_QUERY_1} | mysql
    

提交回复
热议问题