General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 27th, 2007, 01:03 PM
fozzyuw fozzyuw is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 8 fozzyuw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 15 sec
Reputation Power: 0
(seemingly) simple SQL SELECT statement

Hey all,

I admit, I'm very bad with SQL. I'm trying to do something, that I would think would be easy, but I cannot get it right. I know this has to be" SQL 101", but, I've stuck. Here's the scoop.

3 tables.

products
--------
products_id
products_number
(other stuff)...

categories
----------
categories_id
categories_name
(other stuff)...

products_x_categories
----------------------
products_id
categories_id

How would I got about getting the products data from the table 'products' that have been assigned to two different categories (assigned to BOTH categories)?

Example data...

products
--------
1 MMT100
2 MMT200
3 XXT100
4 XXT200

categories
----------
1 "Foo"
2 "Bar"
3 "Goo"

products_x_categories
---------------------
1 1
1 2
2 1
2 3
3 1
3 2
4 3


Query = "Give me the products id and name that belong to BOTH 'foo' and 'bar';

result =
1 MMT100
3 XXT100

So, how would I write that SQL?

Cheers!
Fozzy

*edit* spelling/grammer

Reply With Quote
  #2  
Old February 27th, 2007, 02:41 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 19 m 48 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
I think you want something like this:
MySQL Code:
Original - MySQL Code
    SELECT * FROM products WHERE 2 == (SELECT COUNT(*)     FROM products_x_categories         INNER JOIN categories ON (categories.id = products_x_categories.category_id)     WHERE categories.name IN('foo', 'bar')         AND products_x_categories.product_id = products.id     GROUP BY products_x_categories.product_id);


Didn't test for syntax errors, so it may not work out of the box.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 263

Reply With Quote
  #3  
Old February 27th, 2007, 03:57 PM
fozzyuw fozzyuw is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 8 fozzyuw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 15 sec
Reputation Power: 0
Thanks for the response Itsacon,

I don't think that's exactly what I'm looking for. It appears there's a condition that is (2 == ...) which, in my case, can me anything more than two.

I did find another solution, which required using an extra field as a check, which wasn't used before.

Thanks for the help mate!

Cheers,
Fozzy

Reply With Quote
  #4  
Old February 28th, 2007, 04:50 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2Folding Points: 541313 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 19 m 48 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
the "2 ==" can of course be replaced by "1 <", so you get everything with two or more entries.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > (seemingly) simple SQL SELECT statement


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway