LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2004, 07:12 AM   #1
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Rep: Reputation: 0
MySQL queries from Shell - unexpected result


List,

I have a problem with querying a MySQL database from Shell in batch mode.

I have a file called 'mylist' in this format:
10.156.40.36
10.150.133.33
10.140.159.89


Now with a script I make a Class C of every IP address and put that in a tempfile:
use sitedb
SELECT C FROM sitelist WHERE A LIKE "10.156.40.0";
SELECT C FROM sitelist WHERE A LIKE "10.150.133.0";
SELECT C FROM sitelist WHERE A LIKE "10.140.159.0";

Then I merge the output with the following command:
mysql --silent -h 10.10.10.10 -u username -ppassword < tempfile | paste mylist -

This works extremely well and gives this output:
10.156.40.36 Rome
10.150.133.33 Brussels
10.140.159.89 Amsterdam

However, if for instance 10.156.40.0 (Rome) is not found in the database, I don't get a result for that query (luckily). But my output gets shifted and doesn't match anymore:

10.156.40.36 Brussels
10.150.133.33 Amsterdam
10.140.159.89

In my opinion there are 2 options:
Force MySQL to return 'nill' or something similar if the result of a query is nill (no clue how to do that)

Adapt the script one or the other way so that it checks for a result after each query. Also no clue how to do that

Any suggestion would be greatly appreciated...

Kind regards,

Phil
 
Old 05-04-2004, 10:47 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You can try something like this:
Code:
SELECT '10.156.40.36', C FROM sitelist WHERE A LIKE "10.156.40.0";
SELECT '10.150.133.33', C FROM sitelist WHERE A LIKE "10.150.133.0";
SELECT '10.140.159.89', C FROM sitelist WHERE A LIKE "10.140.159.0";
 
Old 05-04-2004, 11:34 AM   #3
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
Hmmm, I think I miss something here...
If its an unknow ip-address, it still gives nothing as result, while with a known address it gives 2 results?
 
Old 05-04-2004, 04:54 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Try using the IFNULL function -

like
Code:
SELECT  IFNULL( C,'Not found.')  FROM sitelist WHERE A LIKE "10.156.40.0";
Edit : oops used the Oracle syntax.

Last edited by jim mcnamara; 05-04-2004 at 04:58 PM.
 
Old 05-04-2004, 05:52 PM   #5
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
This works if A (10.156.40.0) exists and C is empty (NULL), but NOT if A doesn't exist...
I want 'not found' if A doesn't exist. Is there a similar syntax that does this?

Thx (we're getting close)

- Phil
 
Old 05-04-2004, 06:38 PM   #6
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
Just to clarify, in 'pseudo-code', this would look like:
SELECT C FROM sitelist WHERE IFNOTFOUND( A,'unknown') LIKE "10.156.40.0";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mutlithreaded client slow mysql queries gecoool Programming 3 10-11-2005 02:26 AM
shell - acting on result of calculation corck Programming 3 07-05-2005 10:16 AM
MySQL - Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 9 jasontn Linux - Software 0 02-09-2005 01:17 PM
SQL queries per page (PHP, MySQL) Silent1 Programming 2 06-28-2004 12:15 AM
cp command problem..... unexpected result hamster Linux - General 2 04-10-2003 05:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration