I have a file of comma-separated user ids. And I’d like to SELECT
for these users.
That’s to say I imagine it ought to be looking like this:
mysql> SELECT u.token, u.user_id FROM users u WHERE u.user_id IN (SELECT * LOAD_FILE('/home/alex/lzmigration/users'));
Somehow this LOAD_FILE
thingy does not work. I’m not sure I understand why.
I’m getting this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘LOAD_FILE(‘/home/alex/lzmigration/users’))’ at line 1
Here is the version info.
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.0.35-27 |
+-----------+
2
Answers
To match against a comma-separated string, you must use the
FIND_IN_SET()
function;IN
is for a literal list in the SQL or for a subquery that returns multiple rows.Make sure there are no spaces around the comma in the file, as
FIND_IN_SET()
will not ignore them.Starting with a file
abc.csv
, with the following contents:You could do:
NOTE:
/var/lib/mysql-files/
is the output ofshow variables like '%secure_file_priv%';
which produces:
But then you have to parse that one string into the fields needed, an you wil not have a table defined.
Alternative solution
From the dorectory where your
abc.csv
file is type:NOTE:
output:
When you save this output to a file you basically have the SQL script needed to create the table (including the INSERT statements)
Downside is that all columns will be of TYPE
TEXT
.