Mysql query reading a text file.


 
Thread Tools Search this Thread
Operating Systems Solaris Mysql query reading a text file.
# 1  
Old 12-23-2009
Question Mysql query reading a text file.

Hi,
Is there any way that mysql query reads the content from a text file which has data in the below format:
1,2,3,4,5
and selects matched data from another table.
ie I have a table named xyz which has same ids as in that file.
I want this query to get count of the ids from xyz file by matching the ids.

At mysql prompt i run this command which gets me the count from xyz table.
mysql>select count(*) from xyz where id in (1,2,3,4,5);
Now i want the sql query to read the file and match these ids with ids in xyz table and get me the count.

Image
jyothi_wipro
# 2  
Old 12-23-2009
No
Its not possible.
Mysql client only reads from the db and not from just any text file. You would have to use a mediator like perl to fetch data from both sources and then compare.

---------- Post updated at 04:27 PM ---------- Previous update was at 04:27 PM ----------

No
Its not possible.
Mysql client only reads from the db and not from just any text file. You would have to use a mediator like perl to fetch data from both sources and then compare.
# 3  
Old 12-23-2009
I want to write a script which will connect to db and then in this script i give a query which shld read this data,match and get me the count.
can u tell me how will i use mediator like perl to get the count with some eg.
jyothi_wipro
# 4  
Old 12-23-2009
If it's really just this one line, use
Code:
echo "select count(*) from xyz where id in ($( cat test.txt ));" | mysql -u ....

# 5  
Old 12-23-2009
Quote:
Originally Posted by pludi
If it's really just this one line, use
Code:
echo "select count(*) from xyz where id in ($( cat test.txt ));" | mysql -u ....

cool one but a lot of struggle for further processing.
regards.
# 6  
Old 12-23-2009
Thanks!! that was really exact what i wanted,without any struggle i could get the output which i required.
That was really helpful.Smilie
jyothi_wipro
# 7  
Old 12-23-2009
Of course, you could also go an use the line from your other thread and create something compact but unreadable like
Code:
perl -le 'print "SELECT COUNT(*) FROM xyz WHERE id IN (".(join ",", map { chomp; $_ } <>).");";' file | mysql -u ....

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write MySQL query in text file and run in Linux?

I would like to call a .sql file from a .sh file in linux. The .sql file will contain queries to MySQL database. I am able to call the .sql file by running .sh file in linux, but the sql query is not working. Below is my syntax. The select statement is throwing below error. ./sample.sql: line... (1 Reply)
Discussion started by: qytan
1 Replies

2. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

Replace query by reading the file

Hi Guys, I am having below file which holds data like this file.txt name,id,flag apple,1,Y apple,2,N mango,1,Y mango,2,Y I need to read the above file and frame a query like this hive -s -e "create apple_view as select 1 from main_table;" hive -s -e "create mango_view as select... (11 Replies)
Discussion started by: rohit_shinez
11 Replies

4. Shell Programming and Scripting

To extract query from a text file.

Hi, I've a txt file with the following contents: variable = "select name, emp_id, org_name " variable = variable + " from table_name where " variable = variable + " condition_1 = ABC and " variable = variable + " condition_2 = DEF " varaible = variable + " order by other condition " ... (7 Replies)
Discussion started by: arjun_arippa
7 Replies

5. Programming

reading a text file in c++

hello all , im trying to read a text file and display its contents. While i got the code running and the output was displayed perfectly for sometime , i started getting Abort(core dump) error . Am i missing something here ? im using HP-UX. #include <iostream.h> #include <fstream.h> #include... (1 Reply)
Discussion started by: vishy_85
1 Replies

6. UNIX for Dummies Questions & Answers

Reading from a file and passing the value to a select query

Hi all, Here is my problem. I want to read data from a file and pass the variable to a select query. I tried but it doesn't seem to work. Please advise. Example below. FileName='filekey.txt' while read LINE do var=$LINE print "For File key $var" ${ORACLE_HOME}/bin/sqlplus -s... (1 Reply)
Discussion started by: er_ashu
1 Replies

7. UNIX for Dummies Questions & Answers

Help with reading text file

How can i have a while loop as follows while read inputline do <task> done < name_list and also store the values (delimited) on each line to temp variables so as to print them on screen as follows while read inputline do set name | cut -d "," -f1 name_list # #i know this is not... (1 Reply)
Discussion started by: bilal05
1 Replies

8. Shell Programming and Scripting

Reading text from a file

Guys, I am trying to read text from a file, into arrays. The format of the file is: @DATABASE femotest @PACKAGE_SPECS /usr/home/oracle92/sosa/scripts/test.pks /usr/home/oracle92/sosa/scripts/rep.pks @PACKAGE_BODIES ... (12 Replies)
Discussion started by: LiquidChild
12 Replies

9. Shell Programming and Scripting

reading text file

I have a text file with 50 munbers. I wanna read these numbers and append "@yahoo.com" and send emails to them using shell scripting.......... How do i read the inetegres from the text file. (1 Reply)
Discussion started by: jaan
1 Replies
Login or Register to Ask a Question