We Recommend

SQL Cookbook SQL Cookbook
Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The SQL Cookbook moves quickly from problem to solution, saving you time each step of the way.


Posted By

pablazo on 09/04/07


Tagged

mysql


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

wizard04


Backwards LIKE Statements


Published in: SQL 


URL: http://www.petefreitag.com/item/619.cfm

  1. Sometimes you need TO think backwards.
  2.  
  3. Here was the problem. I needed TO match up some IP address ranges TO the company that owns them. Looking FOR a simple solution TO the problem I came up WITH storing the IP address block patterns IN the DATABASE AS follows:
  4.  
  5. ip_pattern
  6. ----------------
  7. 127.%.%.%
  8. 192.168.%.%
  9. 10.%.%.%
  10.  
  11. Any idea why I choose % AS the wildcard?
  12.  
  13. That's right - it's the wildcard operator IN SQL FOR the LIKE statement.
  14.  
  15. So now when I have have an IP address 192.168.1.1, I can do what I LIKE TO call a backwards LIKE query:
  16.  
  17. SELECT company, ip_pattern
  18. FROM company_blocks
  19. WHERE '192.168.1.1' LIKE ip_pattern
  20.  
  21. This works ON SQL Server AND MySQL, AND I would think it should work fine ON any DATABASE server.

Report this snippet 

You need to login to post a comment.