简体   繁体   中英

MySQL select query to read variable value from file

I have a simple query

select user and class
from student
where school='pass variable from a text file in location /home/user/school.txt'

Text file format:

Nehru Bhavan 
Mahatma Gandhi School

One school name (school name contains space) per row in the text file.

How to do it right.

I tried

echo "select user and class from student where school in ($(cat school.txt));" | mysql -u admin -p database_name

It didn't work.

You can create a temp table with a definition and can import CSV content into it.

 CREATE TABLE #T(Name VARCHAR(200))
    BULK INSERT #T
    FROM 'F:\Test.txt'
    WITH 
      (
        FIELDTERMINATOR = ';', 
        ROWTERMINATOR = '\n' 
      )
    
    SELECT * FROM student WHERE ColumnName IN (SELECT Name FROM #T)

Refer this URL, BULK INSERT in MYSQL for MYSQL Version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM