Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vickp07
Starting Member
2 Posts |
Posted - 2011-09-26 : 17:21:49
|
| Hey guys,so i am having trouble trying to figure out what my select statement should look like. Here is a look at my tables and rows:CREATE TABLE recipes(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(128) NOT NULL UNIQUE,description VARCHAR(1024) NOT NULL,preptime INT NOT NULL,totaltime INT NOT NULL,rating DOUBLE NOT NULL);CREATE TABLE steps(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,recipe_id INT NOT NULL,stepno INT NOT NULL,text VARCHAR(1024) NOT NULL,FOREIGN KEY (recipe_id) REFERENCES recipes(id));CREATE TABLE ingredients(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(45) NOT NULL,unit_id INT NOT NULL,FOREIGN KEY (unit_id) REFERENCES units_of_measure(id));CREATE TABLE units_of_measure(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(20) NOT NULL);CREATE TABLE recipe_ingredient(recipe_id INT NOT NULL,ingredient_id INT NOT NULL,amount DOUBLE NOT NULL,FOREIGN KEY (recipe_id) REFERENCES recipes(id),FOREIGN KEY (ingredient_id) REFERENCES ingredients(id),UNIQUE(recipe_id,ingredient_id));Now what I want to do is (i am trying to just view the data for recipe #1) :SELECT units_of_measure.name, ingredients.name FROM units_of_measure, ingredients, recipesWHERE ***** THIS IS WHERE I AM HAVING TROUBLE I DUNNO HOW TO WRITE THIS WHERE CLAUSE**********ingredients.unit_id = units_of_measure.idAND recipe.id = 1; |
|
|
vickp07
Starting Member
2 Posts |
Posted - 2011-09-26 : 17:22:11
|
| okay so let me explain a lilttle more.....in the recipe_ingredient table it holds the (amount) field which for example is:1 or2 or .5 for 1/2then in the units_of_measure tableit contains (name) which is something like:teaspoon or tablespoonthen in the ingredients table it contains the (name) field which contains something like:saltor eggsThe ingredients.unit_id contains the number that links it to the units_of_measure.idIn the recipe_ingredient tablethe recipe_id and ingredient_id contains the number that links it to recipe.id and ingredients.id |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-09-26 : 17:57:53
|
"AUTO_INCREMENT"Is that MySQL? This is a Microsoft SQL Server forum, so you may not find people here who are able to answer your question I'm afraid.I would have written something like:SELECT M.name, I.nameFROM recipes AS R JOIN recipe_ingredient AS RI ON RI.recipe_id = R.id JOIN ingredients AS I ON I.id = RI.ingredient_id JOIN units_of_measure AS M ON M.id = I.unit_idWHERE R.id = 1 |
 |
|
|
paultech
Yak Posting Veteran
79 Posts |
Posted - 2011-09-27 : 07:59:16
|
| Hii ,I understand that you want to query a certain recipes (#1) and its ingredients this can be done in sqlserver as select recipes.name , recipes.description,recipes.preptime,recipes.totaltime,recipes.rating,ingredients.name ,units_of_measure.name from recipesinner join recipe_ingredienton recipes.id = recipe_ingredient.recipe_idinner join ingredientson recipe_ingredient.ingredient_id = ingredients.idinner join units_of_measureon ingredients.unit_id= units_of_measure.idwhere recipes.id = 1 |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-09-27 : 08:28:25
|
| Is that different to what I suggested? |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2011-09-27 : 08:42:40
|
quote: Originally posted by Kristen Is that different to what I suggested?
Why yes. It allows me to start a spam account with a couple good answers. http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-09-27 : 08:51:02
|
Chuck me your crystal ball as I need to look up next week's lottery numbers |
 |
|
|
|
|
|
|
|