SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

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 ASP Free Forums Sponsor:
  #1  
Old September 12th, 2004, 08:10 PM
bamatick bamatick is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 bamatick User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Cookbook SQL

I have a DB with 3 tables:
dishes (dishID, dishName, calories, etc)
ingredients (ingredID, ingredName, cost, etc)
recipes (recipeID, dishIDFK, ingredIDFK)

What I want is:
1. Spaghetti
a)Noodles
b)Sauce
2. Tuna Surprise
a)Noodles
b)Peas
c)Tuna
.
.
.
N. Dish N
a)Ingredient N

What's the SQL that gives me that?

What I am currently getting with the code I have is:
1. Spaghetti
a)Noodles
2. Spaghetti
b)Sauce
3. Tuna Surprise
a)Noodles
4. Tuna Surprise
b)Peas
5. Tuna Surprise
c)Tuna

Reply With Quote
  #2  
Old September 13th, 2004, 09:57 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
Try providing a sample of your database data so we can determine what data is in what fields.

Reply With Quote
  #3  
Old September 13th, 2004, 10:56 AM
bamatick bamatick is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 bamatick User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Memnoch
Try providing a sample of your database data so we can determine what data is in what fields.

(contents of db)
Table 1: dishes
field 1: dishID (1, 2, 3...)
field 2: dishName (spaghetti, tuna surprise, chicken kiev...)

Table 2: ingredients
field 1: ingredientID (1, 2, 3,...)
field 2: ingredientName (noodles, tomato sauce, tuna, chicken, flour...)

Table 3: recipes
field 1: recipeID (1, 2, 3,...)
field 2: dishIDFK (corresponding to above)
field 3: ingredientIDFK (corresponding to appropriate dish)

Reply With Quote
  #4  
Old September 13th, 2004, 10:58 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
Let me try being more clear.
Post some sample data, means post what is actually in the fields in your database.

Reply With Quote
  #5  
Old September 15th, 2004, 07:17 AM
bamatick bamatick is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 bamatick User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Memnoch
Let me try being more clear.
Post some sample data, means post what is actually in the fields in your database.


Let me try to be more clear:
Here are the contents of table 1, "dishes":
dishesID...dishName
1...spahetti
2...tuna surprise

Here are the contents of table 2, "ingredients":
ingredID...ingredName
1...noodles
2...tomato sauce
3...peas
4...tuna

Here are the contents of table 3, "recipes":
recipeID...dishIDFK...ingredIDFK
1...1...1
2...1...2
3...2...1
3...2...3
3...2...4

Do the dots help? Or do you actually have to see it in cells?

-----------------------
dishID | dishName |
-----------------------
1 | spaghetti |
-----------------------
2 | tuna surprise|
-----------------------

------------------------
ingredID | ingredName |
------------------------
1 | noodles |
------------------------
2 | tomato sauce|
------------------------
3 | peas |
------------------------
4 | tuna |
------------------------

----------------------------------
recipeID | dishIDFK | ingredIDFK |
----------------------------------
1 | 1 | 1 |
----------------------------------
2 | 1 | 2 |
----------------------------------
3 | 2 | 1 |
----------------------------------
4 | 2 | 3 |
----------------------------------
5 | 2 | 4 |
----------------------------------

Last edited by bamatick : September 15th, 2004 at 07:33 AM. Reason: tired of re-explaining the same thing over and over...

Reply With Quote
  #6  
Old September 15th, 2004, 01:32 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
You won't be able to return it in a Parent/Child format.
But here is the query.
Code:
SELECT A.dishName, C.ingredName
FROM dishes As A
INNER JOIN recipes As B On (A.dishID = B.dishID)
INNER JOIN ingredients As C On (B.ingredID = C.ingredID)
GROUP BY A.dishName, C.ingredName
ORDER BY A.dishName, C.ingredName

Reply With Quote
  #7  
Old September 15th, 2004, 04:16 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
You are going to have to use code to build what you are after,

Or put Menoch SQL into a report where you hide duplicate(repeating) fields
thus giving you something like

spahetti Noodles
tomato sauce
tuna surprise peas
tuna

OR use groupings in your report


The problem is SQL can't give you what you want, you have to take what SQL gives and make it work in a report.

S-


Menoch, Get an extra hour of sleep today? You sure are patient today.

Reply With Quote
  #8  
Old September 15th, 2004, 06:12 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
I'm getting too tired to lose my patience with people.

But tomorrow is another day and I will be back to my old "Impatient" self!

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Cookbook SQL


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT