Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select record where field contains a specific IP - mysql 1

Status
Not open for further replies.

kp2a

ISP
May 26, 2006
7
0
0
US
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> 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 . . .
 
Hi

[ul]
[li]In regular expressions the dot ( [tt].[/tt] ) is a metacharacter meaning any character. [highlight #fcc]Escape them[/highlight] with backslash ( [tt]\[/tt] ).[/li]
[li]To avoid getting matches which start with the give expression but continue with more digits, [highlight #ccf]put a word boundary mark[/highlight] ( [tt][[:>:]][/tt] ) after.[/li]
[/ul]
Code:
[b]select[/b] d07 [b]from[/b] service [b]where[/b] d07 [b]regexp[/b] [green][i]'^10[/i][/green][lime][i][highlight #fcc]\[/highlight].[/i][/lime][green][i]100[/i][/green][lime][i][highlight #fcc]\[/highlight].[/i][/lime][green][i]1[/i][/green][lime][i][highlight #fcc]\[/highlight].[/i][/lime][green][i]1[highlight #ccf][[:>:]][/highlight]'[/i][/green][teal];[/teal]

Feherke.
[link feherke.github.com/][/url]
 
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
 
Hi

Sorry. No idea what I did before that I not observed the evident : [highlight #fcc]additional escaping[/highlight] is needed due to the string syntax :
Code:
[b]select[/b] d07 [b]from[/b] service [b]where[/b] d07 [b]regexp[/b] [green][i]'^10[/i][/green][lime][i][highlight #fcc]\[/highlight]\[/i][/lime][green][i].100[/i][/green][lime][i][highlight #fcc]\[/highlight]\[/i][/lime][green][i].1[/i][/green][lime][i][highlight #fcc]\[/highlight]\[/i][/lime][green][i].1[[:>:]]'[/i][/green][teal];[/teal]

Feherke.
[link feherke.github.com/][/url]
 
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:
[blue][b]SELECT[/b][/blue] * [blue][b]FROM[/b][/blue] [green]conections[/green] [blue][b]WHERE[/b][/blue] [green]conecttext[/green] [blue][b]LIKE[/b][/blue] [red][b]"10.100.1.1 %"[/b][/red]

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
 
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[highlight] [/highlight]%' may make it skip valid records too.

Feherke.
[link feherke.github.com/][/url]
 
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
 
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



 
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


 
! 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




 
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> 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!
 

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.
 
thanks but

Code:
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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top