473,791 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,1 0);

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 3322
Funnyweb (r_*******@eggs tra.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,1 0);

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('1 0', 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**@REMOVEbun dook.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,1 0);

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*******@attgl obal.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
1821
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 info is drawn out of the database. Some of the information is drawn out and displayed properly, but other information is not. The strangest part is if I do a response.write to display the objRS() from the database of a field that isn't coming...
0
3531
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 form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
5
4014
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 fieldX=valueX WHERE id IN (SELECT tX.id FROM tableX AS tX WHERE tX.fieldY=valueY AND filedZ=valueZ);
16
3194
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
3599
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 on how to resolve the stack trace. Resolved stack trace is much more helpful in diagnosing the problem, so please do resolve it Trying to get some variables. Some pointers may be invalid and cause the dump to abort...
8
5483
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
4
2298
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' Help me to sort this out, it has taken me almost a week to wade through and it still won't work. Select Case Forms("frmForce").OpenArgs
48
3881
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 query of all students in both tables with no duplicates. No clue whatsoever.
0
5577
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.