Bluehost.com Web Hosting $6.95

Find result that matches ALL values in array?

This is a discussion on Find result that matches ALL values in array? within the MySQL Database forums, part of the Database Forums category; Using an IN query, I am able to pass something like this to my database to get the values that ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-15-2007
Justin Williams
 
Posts: n/a
Default Find result that matches ALL values in array?

Using an IN query, I am able to pass something like this to my
database to get the values that match at least one of the array
values.

SELECT * FROM contacts WHERE (work_lat != '' and work_long != '' and
id in (select tutor_id from subjects_tutors where subject_id in
(17,18,22,26,27,35))) AND ( (contacts.`type` = 'Tutor' ) )

What I need to do is have the query search for results that match all
array values. In other words, in the example above I need a tutor who
matches subject_id 17,18,22,26,27 and 35. The data model is for a
Ruby on Rails application I am developing for a client.

Any help would be appreciated.

Thank you!

-
Justin Williams
Owner, Second Gear
http://secondgearllc.com/
-
Check out Porchlight: bug tracking for small teams <http://
www.porchlightnow.com>

Reply With Quote
  #2 (permalink)  
Old 02-15-2007
Paul Lautman
 
Posts: n/a
Default Re: Find result that matches ALL values in array?

Justin Williams wrote:
> Using an IN query, I am able to pass something like this to my
> database to get the values that match at least one of the array
> values.
>
> SELECT * FROM contacts WHERE (work_lat != '' and work_long != '' and
> id in (select tutor_id from subjects_tutors where subject_id in
> (17,18,22,26,27,35))) AND ( (contacts.`type` = 'Tutor' ) )
>
> What I need to do is have the query search for results that match all
> array values. In other words, in the example above I need a tutor who
> matches subject_id 17,18,22,26,27 and 35. The data model is for a
> Ruby on Rails application I am developing for a client.
>
> Any help would be appreciated.
>
> Thank you!
>
> -
> Justin Williams
> Owner, Second Gear
> http://secondgearllc.com/
> -
> Check out Porchlight: bug tracking for small teams <http://
> www.porchlightnow.com>


Instead of a subselect (which you almost never have to use), use a series of
joins thus:

SELECT *
FROM `contacts` `c`
JOIN `subjects_tutors` `st1` ON `c`.`id` = `st1`.`tutor_id` AND
`st1`.`subject_id` = 17
JOIN `subjects_tutors` `st2` ON `c`.`id` = `st2`.`tutor_id` AND
`st2`.`subject_id` = 18
JOIN `subjects_tutors` `st3` ON `c`.`id` = `st3`.`tutor_id` AND
`st3`.`subject_id` = 22
JOIN `subjects_tutors` `st4` ON `c`.`id` = `st4`.`tutor_id` AND
`st4`.`subject_id` = 26
JOIN `subjects_tutors` `st5` ON `c`.`id` = `st5`.`tutor_id` AND
`st5`.`subject_id` = 27
JOIN `subjects_tutors` `st6` ON `c`.`id` = `st6`.`tutor_id` AND
`st6`.`subject_id` = 35
WHERE `c`.`work_lat` != '' and `c`.`work_long` != '' AND `c`.`type` =
'Tutor'



Reply With Quote
  #3 (permalink)  
Old 02-16-2007
Justin Williams
 
Posts: n/a
Default Re: Find result that matches ALL values in array?

Well, the values will be dynamically populated based on the query
passed by the Rails application, and it seems somewhat messy to build
x number of JOINs like that when the number of subject_id's passed is
variable. Is there another way that may work better?

Thanks.

- j


On Feb 15, 4:49 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
> Justin Williams wrote:
> > Using an IN query, I am able to pass something like this to my
> > database to get the values that match at least one of the array
> > values.

>
> > SELECT * FROM contacts WHERE (work_lat != '' and work_long != '' and
> > id in (select tutor_id from subjects_tutors where subject_id in
> > (17,18,22,26,27,35))) AND ( (contacts.`type` = 'Tutor' ) )

>
> > What I need to do is have the query search for results that match all
> > array values. In other words, in the example above I need a tutor who
> > matches subject_id 17,18,22,26,27 and 35. The data model is for a
> > Ruby on Rails application I am developing for a client.

>
> > Any help would be appreciated.

>
> > Thank you!

>
> > -
> > Justin Williams
> > Owner, Second Gear
> >http://secondgearllc.com/
> > -
> > Check out Porchlight: bug tracking for small teams <http://
> >www.porchlightnow.com>

>
> Instead of a subselect (which you almost never have to use), use a series of
> joins thus:
>
> SELECT *
> FROM `contacts` `c`
> JOIN `subjects_tutors` `st1` ON `c`.`id` = `st1`.`tutor_id` AND
> `st1`.`subject_id` = 17
> JOIN `subjects_tutors` `st2` ON `c`.`id` = `st2`.`tutor_id` AND
> `st2`.`subject_id` = 18
> JOIN `subjects_tutors` `st3` ON `c`.`id` = `st3`.`tutor_id` AND
> `st3`.`subject_id` = 22
> JOIN `subjects_tutors` `st4` ON `c`.`id` = `st4`.`tutor_id` AND
> `st4`.`subject_id` = 26
> JOIN `subjects_tutors` `st5` ON `c`.`id` = `st5`.`tutor_id` AND
> `st5`.`subject_id` = 27
> JOIN `subjects_tutors` `st6` ON `c`.`id` = `st6`.`tutor_id` AND
> `st6`.`subject_id` = 35
> WHERE `c`.`work_lat` != '' and `c`.`work_long` != '' AND `c`.`type` =
> 'Tutor'



Reply With Quote
  #4 (permalink)  
Old 02-16-2007
Captain Paralytic
 
Posts: n/a
Default Re: Find result that matches ALL values in array?

On 16 Feb, 03:49, "Justin Williams" <carpea...@gmail.com> wrote:
> Well, the values will be dynamically populated based on the query
> passed by the Rails application, and it seems somewhat messy to build
> x number of JOINs like that when the number of subject_id's passed is
> variable. Is there another way that may work better?
>
> Thanks.
>
> - j
>
> On Feb 15, 4:49 pm, "Paul Lautman" <paul.laut...@btinternet.com>
> wrote:
>
>
>
> > Justin Williams wrote:
> > > Using an IN query, I am able to pass something like this to my
> > > database to get the values that match at least one of the array
> > > values.

>
> > > SELECT * FROM contacts WHERE (work_lat != '' and work_long != '' and
> > > id in (select tutor_id from subjects_tutors where subject_id in
> > > (17,18,22,26,27,35))) AND ( (contacts.`type` = 'Tutor' ) )

>
> > > What I need to do is have the query search for results that match all
> > > array values. In other words, in the example above I need a tutor who
> > > matches subject_id 17,18,22,26,27 and 35. The data model is for a
> > > Ruby on Rails application I am developing for a client.

>
> > > Any help would be appreciated.

>
> > > Thank you!

>
> > > -
> > > Justin Williams
> > > Owner, Second Gear
> > >http://secondgearllc.com/
> > > -
> > > Check out Porchlight: bug tracking for small teams <http://
> > >www.porchlightnow.com>

>
> > Instead of a subselect (which you almost never have to use), use a series of
> > joins thus:

>
> > SELECT *
> > FROM `contacts` `c`
> > JOIN `subjects_tutors` `st1` ON `c`.`id` = `st1`.`tutor_id` AND
> > `st1`.`subject_id` = 17
> > JOIN `subjects_tutors` `st2` ON `c`.`id` = `st2`.`tutor_id` AND
> > `st2`.`subject_id` = 18
> > JOIN `subjects_tutors` `st3` ON `c`.`id` = `st3`.`tutor_id` AND
> > `st3`.`subject_id` = 22
> > JOIN `subjects_tutors` `st4` ON `c`.`id` = `st4`.`tutor_id` AND
> > `st4`.`subject_id` = 26
> > JOIN `subjects_tutors` `st5` ON `c`.`id` = `st5`.`tutor_id` AND
> > `st5`.`subject_id` = 27
> > JOIN `subjects_tutors` `st6` ON `c`.`id` = `st6`.`tutor_id` AND
> > `st6`.`subject_id` = 35
> > WHERE `c`.`work_lat` != '' and `c`.`work_long` != '' AND `c`.`type` =
> > 'Tutor'- Hide quoted text -

>
> - Show quoted text -


Messy???
So you program a loop to create all the joins from a single pattern.
You never even see the resultant query so you don't see any mess. The
only foreseeable problem is reaching the maximum number of joins
allowed for your particular installation.

Reply With Quote
  #5 (permalink)  
Old 02-16-2007
Felix Geerinckx
 
Posts: n/a
Default Re: Find result that matches ALL values in array?

"Justin Williams" <carpeaqua@gmail.com> wrote in
news:1171577188.644550.5490@k78g2000cwa.googlegrou ps.com:

> Using an IN query, I am able to pass something like this to my
> database to get the values that match at least one of the array
> values.
> ...
> What I need to do is have the query search for results that match all
> array values. In other words, in the example above I need a tutor who
> matches subject_id 17,18,22,26,27 and 35.


SELECT
tutor_id
FROM subjects_tutors
WHERE
subject_id IN (17,18,22,26,27,35)
GROUP BY tutor_id
HAVING COUNT(subject_id) = 6; -- 6 = number of elements in IN-list
Reply With Quote
  #6 (permalink)  
Old 02-23-2007
Malcolm Dew-Jones
 
Posts: n/a
Default Re: Find result that matches ALL values in array?

Justin Williams (carpeaqua@gmail.com) wrote:
: Using an IN query, I am able to pass something like this to my
: database to get the values that match at least one of the array
: values.

: SELECT * FROM contacts WHERE (work_lat != '' and work_long != '' and
: id in (select tutor_id from subjects_tutors where subject_id in
: (17,18,22,26,27,35))) AND ( (contacts.`type` = 'Tutor' ) )

: What I need to do is have the query search for results that match all
: array values. In other words, in the example above I need a tutor who
: matches subject_id 17,18,22,26,27 and 35. The data model is for a
: Ruby on Rails application I am developing for a client.

: Any help would be appreciated.


Read up on the ANY IN SOME and ALL keywords which can be used as part of
the WHERE clause of a select.


13.2.8.3 Subqueries with ANY, IN, and SOME
13.2.8.4 Subqueries with ALL

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:40 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0