473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with trick MySQL select problem

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

Jul 17 '05 #1
3 3306
Funnyweb (r_*******@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 <da**@REMOVEbundook.com>
(Remove REMOVE for email address)
Jul 17 '05 #2
Funnyweb wrote:
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

Hamilton,

Try reading up on database normalization. Then create a link table with
two columns. First is the primary key of the original table, second is
a *single* integer. Multiple rows can have the same first column but
the second column would chance.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #3
Hamilton is right.

If you have tables like this:

users
int userid
varchar name
bit active

books
int bookid
varchar name

Instead of having another field in users to hold all bookids that they
have read, do this:

read_book
int userid
int bookid

With 1 row for each book each user read. Then you can select all rows
from read_book where userid=x and get the book info for each row.

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Munzilla | last post by:
Ok, I have an ASP page that I use to add several pieces of information to a database and also display that information in an edit mode. The problem is, when I use the page for edit, not all of the...
0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
5
by: red85 | last post by:
hello i have mysql 4.1 with win2000 SP3, i know that it is only an alpha and i don't know if someone else has already posted this problem: when i execute this sql UPDATE tableX SET...
16
by: Andie | last post by:
Hello All, I have this sql statement thats works in MSSQL when I call a function function showNewProd(dispNum) 'Declare some variables dim mySQL, rsTemp, tempStr, count 'Read Database
2
by: pratchaya | last post by:
This is my sample error in my MySQL Log New value of fp=(nil) failed sanity check, terminating stack trace! Please read http://www.mysql.com/doc/en/Using_stack_trace.html and follow instructions...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: Mark | last post by:
the Following bit of code doesn't work. It seems to respond to the second, starting with 'add iif statement for Good Practice', but not to the first, starting 'add iif statement for archived' ...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.