MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 8th, 2007, 04:56 PM
tabbycat tabbycat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 63 tabbycat User rank is Lance Corporal (50 - 100 Reputation Level)tabbycat User rank is Lance Corporal (50 - 100 Reputation Level)tabbycat User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 16 h 37 m 11 sec
Reputation Power: 9
"AND" select on single column

I am trying to write a select that finds all objects that match a set of values for a column. In using the sample tables below, an "object" has "color" values that are stored in the table "object_color". An object can have zero to many colors.

I would want to select all objects that have a combination of colors. For example, I want to select all objects that are yellow, red, and green. Is such an "and" search possible in one select?


OBJECT
--------
object_id
(other attributes)

COLOR
-------------
color_id
(other attributes)

OBJECT_COLOR
----------------
object_id
color_id

Reply With Quote
  #2  
Old August 8th, 2007, 06:48 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
the full query:
Code:
select OBJECT.object_id
     , OBJECT.other_attributes
  from COLOR
inner
  join OBJECT_COLOR
    on OBJECT_COLOR.color_id = COLOR.color_id 
inner
  join OBJECT
    on OBJECT.object_id = OBJECT_COLOR.object_id
 where COLOR.color_name in ( 'yellow' , 'red' , 'green' )
group
    by OBJECT.object_id
having count(distinct OBJECT_COLOR.color_id) = 3


if the color_ids are known, as they would be if they were selected from a dropdown with the MULTIPLE attribute, you can simplify to:
Code:
select OBJECT.object_id
     , OBJECT.other_attributes
  from OBJECT_COLOR
inner
  join OBJECT
    on OBJECT.object_id = OBJECT_COLOR.object_id
 where OBJECT_COLOR.color_id in ( 14, 9, 37 )
group
    by OBJECT.object_id
having count(distinct OBJECT_COLOR.color_id) = 3
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old August 9th, 2007, 11:36 AM
tabbycat tabbycat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 63 tabbycat User rank is Lance Corporal (50 - 100 Reputation Level)tabbycat User rank is Lance Corporal (50 - 100 Reputation Level)tabbycat User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 16 h 37 m 11 sec
Reputation Power: 9
Would I be able to merge that query with another "or" criteria. For example, a "SIZE" table structured the same as "COLOR". Would the following be correct?

select O.OBJECT_ID
from OBJECT as O
left join OBJECT_COLOR as OC on OC.OBJECT_ID=O.OBJECT_ID
left join OBJECT_SIZE as OS on OS.OBJECT_ID=O.OBJECT_ID
where OC.COLOR_ID in (1,3,9) and OS.SIZE_ID in (2,7)
group by O.OBJECT_ID having count (distinct OC.COLOR_ID)=3

Also, I tend to use left join. Would there be a difference compared to using inner join as you showed?

Reply With Quote
  #4  
Old August 9th, 2007, 04:56 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by tabbycat
Would the following be correct?
it might be -- try it and see

Quote:
Originally Posted by tabbycat
Also, I tend to use left join. Would there be a difference compared to using inner join as you showed?
ordinarily, yes, there is a big difference between LEFT OUTER JOIN and INNER JOIN

however, in a LEFT OUTER JOIN, if you have a condition in the WHERE clause on any column from the right table, then all unmatched left table rows will be tossed out, consequently the query will actually behave as an inner join

test your query, then test this one and tell me if there is any difference --
Code:
select O.OBJECT_ID
from OBJECT as O 
left join OBJECT_COLOR as OC 
on OC.OBJECT_ID=O.OBJECT_ID and OC.COLOR_ID in (1,3,9)
left join OBJECT_SIZE as OS 
on OS.OBJECT_ID=O.OBJECT_ID and OS.SIZE_ID in (2,7)
group by O.OBJECT_ID 
having count (distinct OC.COLOR_ID)=3

Reply With Quote
  #5  
Old August 10th, 2007, 07:43 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
just trying the alternate code formatter:
sql Code:
Original - sql Code
  1. SELECT O.OBJECT_ID
  2. FROM OBJECT AS O
  3. LEFT JOIN OBJECT_COLOR AS OC
  4. ON OC.OBJECT_ID=O.OBJECT_ID AND OC.COLOR_ID IN (1,3,9)
  5. LEFT JOIN OBJECT_SIZE AS OS
  6. ON OS.OBJECT_ID=O.OBJECT_ID AND OS.SIZE_ID IN (2,7)
  7. GROUP BY O.OBJECT_ID
  8. HAVING COUNT (DISTINCT OC.COLOR_ID)=3

Reply With Quote
  #6  
Old August 10th, 2007, 09:08 AM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,048 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 2 Days 9 h 11 m 14 sec
Reputation Power: 1303
Quote:
Originally Posted by r937
just trying the alternate code


After all these years? Hey people come look, Rudy moved to the dark side!

Reply With Quote
  #7  
Old August 10th, 2007, 09:21 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
no, actually, i'm sticking with code tags

somebody on another forum wanted to see an example of the formatting produced by the language-specific highlighter

i searched the forum for a while, couldn't find one, and finally decided just to create one myself


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > "AND" select on single column


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap