INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Select record where field contains a specific IP - mysql

Select record where field contains a specific IP - mysql

Select record where field contains a specific IP - mysql

(OP)
Select a record where a field contains a specific IP.

where field regexp IP
gives multiple matches
I do not have clue as to the unexpected results

CODE --> mysql

mysql> select d07 from service where d07 regexp '^10.100.1.1';
+------------------------------------------------+
| d07                                            |
+------------------------------------------------+
| 10.100.101.186 on at 2011-06-20 02:12:02       |
| 10.100.101.188 at 2009-05-12 05:12:47          |
| 10.100.101.251 at 2009-03-26 16:00:37          |
| 10.100.101.182 at 10282300                     |
| 10.100.101.70 on at 2011-04-25 02:16:27        |
| 10.100.101.59 off since 2012-02-18 18:52:04    |
| 10.100.101.229 on at 2011-10-17 22:40:56       |
| 10.100.101.107 at 2009-12-01 06:09:32          |
| 10.100.101.77 at 2009-05-31 05:10:32           |
| 10.100.101.92                                  |
| 10.100.101.91 off at 2010-08-07 10:07:57       |
| 10.100.101.171                                 |

. . . and on and on . . . 

RE: Select record where field contains a specific IP - mysql

Hi

  • In regular expressions the dot ( . ) is a metacharacter meaning any character. Escape them with backslash ( \ ).
  • To avoid getting matches which start with the give expression but continue with more digits, put a word boundary mark ( [[:>:]] ) after.

CODE --> MySQL

select d07 from service where d07 regexp '^10\.100\.1\.1[[:>:]]'; 

Feherke.
http://feherke.github.com/

RE: Select record where field contains a specific IP - mysql

(OP)
thanks
I have tried that - and also [[:<:]] . . . [[:>:]] - same results
also with and without the escape \ - same results
note that NONE of the "matches" match the first 6 characters
I am bewildered!

mysql> select d07 from service where d07 regexp '^10\.100\.1\.1[[:>:]]';
+------------------------------------------------+
| d07 |
+------------------------------------------------+
| 10.100.101.186 on at 2011-06-20 02:12:02 |
| 10.100.101.188 at 2009-05-12 05:12:47 |
| 10.100.101.251 at 2009-03-26 16:00:37 |
| 10.100.101.182 at 10282300 |
| 10.100.101.70 on at 2011-04-25 02:16:27 |
| 10.100.101.59 off since 2012-02-18 18:52:04 |

. . .
and so on

RE: Select record where field contains a specific IP - mysql

Hi

Sorry. No idea what I did before that I not observed the evident : additional escaping is needed due to the string syntax :

CODE --> MySQL

select d07 from service where d07 regexp '^10\\.100\\.1\\.1[[:>:]]'; 

Feherke.
http://feherke.github.com/

RE: Select record where field contains a specific IP - mysql

Personally for such a simple match I would use a simple LIKE statement. If you are just trying to match the field with the IP 10.100.1.1

CODE -->

SELECT * FROM conections WHERE conecttext LIKE "10.100.1.1 %" 


As a note, this is not the fastest method, and will likely be very slow with large recordsets, but an index may help with that.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Select record where field contains a specific IP - mysql

Hi

Yes, but in the opening post there was no visible text after 10.100.101.92 and 10.100.101.171, so that space in '10.100.1.1 %' may make it skip valid records too.

Feherke.
http://feherke.github.com/

RE: Select record where field contains a specific IP - mysql

I was under the impression, none of those matches were valid and that the OP was looking for the exact address 10.100.1.1 in which case the IP ends at that position and based on what matches were found there is always a space after the IP address.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Select record where field contains a specific IP - mysql

(OP)
thanks for the suggestions
with a little "hacking" around the suggestions of using LIKE
I think I found a solution but do not know why it works.
And still do not know why regexp fails.
So two open questions!

My solution based upon your suggestions.
The earlier suggestion does not work because there is not necessarily a space after the IP.

Apparently LIKE given a "word" works on word boundaries?


<code>

mysql> select state,d09,d07 from service where d07 like '10.100.1.1%';
+-------+--------------------------+--------------+
| state | d09 | d07 |
+-------+--------------------------+--------------+
| 0 | cr1.pullpnt 25512 | 10.100.1.158 |
| 0 | Link PP2Prn 25506 | 10.100.1.157 |
| 0 | Link PP2Sol 25505 | 10.100.1.130 |
| 0 | CV2SOL 26754 | 10.100.1.138 |
| 0 | Link Sol2CV 26775 | 10.100.1.139 |
| 0 | Link Sol2PP 27014 | 10.100.1.131 |
| 0 | Link Princess2Pull 27016 | 10.100.1.154 |
| 0 | br1.glbx 28281 | 10.100.1.1 |
+-------+--------------------------+--------------+
8 rows in set (0.02 sec)

mysql> select state,d09,d07 from service where d07 like '10.100.1.1';
+-------+----------------+------------+
| state | d09 | d07 |
+-------+----------------+------------+
| 0 | br1.glbx 28281 | 10.100.1.1 |
+-------+----------------+------------+
1 row in set (0.02 sec)

</code>

just to make sure of my assumption of word boundary
I added a test case;
<code>
mysql> select state,d09,d07 from service where d07 like '110.100.1.1';
+-------+-------------------+-------------+
| state | d09 | d07 |
+-------+-------------------+-------------+
| 1 | John Ackley 25538 | 110.100.1.1 |
+-------+-------------------+-------------+
1 row in set (0.02 sec)

mysql> select state,d09,d07 from service where d07 like '10.100.1.1';
+-------+----------------+------------+
| state | d09 | d07 |
+-------+----------------+------------+
| 0 | br1.glbx 28281 | 10.100.1.1 |
+-------+----------------+------------+
1 row in set (0.02 sec)
</code>

looks like default is to search on word boundary!?

I RTFM and found nothing apropos!
Comments?

A hymn to Saraswati, the Goddess of Knowledge:
Wonderful is your gift of knowledge
the more we share, the more it grows
the more we hoard it, the more it diminishes



RE: Select record where field contains a specific IP - mysql

Please correct me if I am mistaken...

I would read

select state,d09,d07 from service where d07 like '110.100.1.1' to be the same as

select state,d09,d07 from service where d07 = '110.100.1.1'

I would expect select state,d09,d07 from service where d07 like '110.100.1.1%' to give me everything starting with "110.100.1.1" including IPs with .1X or .1XX as the last octet.

If I wanted to get everything where there may or may not be a space after the last .1 and only the .1 records, I'm thinking I would use something like

select state,d09,d07 from service where (d07 = '110.100.1.1' or d07 like '110.100.1.1 %')

The parenthesis for grouping may be needed if additional where conditions are desired.

IndianaSkinny


RE: Select record where field contains a specific IP - mysql

(OP)
! you are correct ! thanks !
verified by further testing
call the search term in quotes a "word"
with no qualifies it appears that LIKE and = produce the same result!
(but watch out for white space!)

My original task remains unsolved: how to match fields that contain an unique IP which may or may not be embedded in other data in a field (column) but separated by spaces if other data is included. I think problems with LIKE is what motivated me ten years ago when I first wrote the code to use regexp - only recently have I observed problems with this code that has been running so long!

Unless someone has a better idea, I will go back to playing with regexp! Or rewrite the application!

A hymn to Saraswati, the Goddess of Knowledge:
Wonderful is your gift of knowledge
the more we share, the more it grows
the more we hoard it, the more it diminishes




RE: Select record where field contains a specific IP - mysql

(OP)
I have a solution for my case - IP first position in field
may be the entire field or followed with space and possibly more data or nothing.
(just translate the above to regexp!)
wink

CODE --> mysql

mysql> select d07 from service where d07 regexp '^10.100.1.1 |^10.100.1.1$';
+--------------------+
| d07                |
+--------------------+
| 10.100.1.1         |
| 10.100.1.1 testing |
+--------------------+
2 rows in set (0.11 sec) 

thanks to all for suggestions!

RE: Select record where field contains a specific IP - mysql


select d07 from service where if(instr(d07,' ') > 0, left(d07,instr(d07,' ')),d07) = '10.101.1.1' ;

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.

RE: Select record where field contains a specific IP - mysql

(OP)
thanks but

CODE --> mysql

mysql> select d07 from service where if(instr(d07,' ') > 0, left(d07,instr(d07,' ')),d07) = '10.101.1.1' ;
Empty set (0.02 sec)

mysql> select d07 from service where d07 regexp '^10.100.1.1 |^10.100.1.1$';
+--------------------+
| d07                |
+--------------------+
| 10.100.1.1         |
| 10.100.1.1 testing |
+--------------------+
2 rows in set (0.11 sec) 

note also
IP first position in field
may be the entire field or followed with space and possibly more data or nothing.

RE: Select record where field contains a specific IP - mysql

Yes, sorry, I used IP addresses I had in stock, check the numbers :) -- you should change 101 in my query for 100 as in yours.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close