Re: MINUS in MySQL (was Re: Thanks!) -- also intesection with LIKE?

From: Kohler, Andy <akohler_at_nyob>
Date: Fri, 26 Jan 2007 09:23:42 -0800
To: CODE4LIB_at_listserv.nd.edu
Ken Irwin asked:
> I wonder if it's possible to use LIKE with
> the results of a subquery, eg.:
> SELECT * FROM table WHERE ip [NOT LIKE ANYTHING IN] (SELECT
> ip_range FROM known_ips) where [NOT LIKE ANYTHING IN] is
> probably some different wording.

In general, you'd do this like (hah):

SELECT *
FROM table t
WHERE NOT EXISTS
(  SELECT *
   FROM known_ips
   WHERE ip = t.ip
)

> I have script that combs through our logs to weed out
> spiders, bots and whatnot, and it references a table full of
> known good IPs that are definitely real users. Right now I
> have this hideous long query that includes a "WHERE ip not
> like '136.227.%' and ip not like '123.345.%'
> and...". If there's a way to similarly slim down this
> statement, I would love to find it.

Unfortunately, SQL doesn't know anything about ip ranges.  But it looks
like your known_ips table contains individual ip addresses, not ranges,
so the above (or something similar) should work.

Andy Kohler / UCLA Library Info Technology
akohler_at_library.ucla.edu / +1 310 206 8312
Received on Fri Jan 26 2007 - 11:51:10 EST