You are here: Re: Help with trick MySQL select problem « PHP Programming Language « IT news, forums, messages
Re: Help with trick MySQL select problem

Posted by Dave on 05/26/05 05:50

Funnyweb (r_rachnid@eggstra.co.au) decided we needed to hear...
> I have a database table, which has field that could contain a single integer
> or a list of comma separated integers.
>
> Is it possible to match each row of that field against an array of integers
> and return those rows where any of the integers in that field are in my
> array?
>
> For example suppose I did the following:
>
> arInts = array(2,4,6,8,10);
>
> What I want to do is return all rows from my table where my field contains a
> single integer and it is in my array, or where my field contains a list of
> integers and any of those integers is in my array?
>
> Is this possible and if so how do I do it?
>
> Thanks
>
> Hamilton

Closest you could get in pure SQL would probably be...

select * from table where
field_in_set('2', arraycol) > 0 or
field_in_set('4', arraycol) > 0 or
field_in_set('6', arraycol) > 0 or
field_in_set('8', arraycol) > 0 or
field_in_set('10', arraycol) > 0

arraycol is your column with the comma separated ints, and '2',
'4' etc are the values from the array. You would have to generate
the query via a loop over your array, but of course if the array
is large, then the query will also be large!

How big is the table? If it is small, how likely is it to grow?
I ask because you might be better off selecting all rows, then
processing the result set in a loop, discarding rows not matching
your array - use a combination of explode and array_intersect.

--
Dave <dave@REMOVEbundook.com>
(Remove REMOVE for email address)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация