Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-19-2009, 12:42 PM   PM User | #1
ljmyers
New to the CF scene

 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ljmyers is an unknown quantity at this point
Smile Select Statement - Two Tables

My apologies if the answer to this is here somewhere. I have searched for days and tried writing it 500 different ways trying to figure this out but cannot get it to work just as i want it to. I have two tables, prefix_users and prefix_recipes. I need for the statement to look at and compare the id (user's id) from table prefix_users with the user_id and id (recipe's id) from the table prefix_recipes and if they are the same, show the recipe's name on the page. It is the user's profile page that I am trying to include the recipes that the user has submitted on their specific page. Any help would be great.

table: prefix_users
id (user's id)

table: prefix_recipes
id (recipe's id)
user_id
name (recipe's name)

Smiles,
Lana
ljmyers is offline   Reply With Quote
Old 02-19-2009, 01:20 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is actually an SQL question, not a PHP one.
I don't understand what you're trying to do with this though. From the sounds of you're rules, you're looking to lookup WHERE users.id = recipes.id = recipes.user_id?
Anyway, simple where clauses for those:
Code:
SELECT `name` FROM `prefix_recipes` WHERE `id` = {id} AND `user_id` = {id}
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-19-2009, 01:39 PM   PM User | #3
ljmyers
New to the CF scene

 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ljmyers is an unknown quantity at this point
Thanks so much for the time you took Fou-Lu. I knew it was something simple and I have little knowledge with sql statements and for some reason, could not wrap my brain around it.
ljmyers is offline   Reply With Quote
Old 02-20-2009, 12:00 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
np, is that what you're looking for though? The logic of you're table schema seems to apply the same foreign key to the values of two seemingly unrelated fields, which will likely provide inaccurate results. I'm thinking you're only wanting to compare on a user id, so you can fetch all recipes owned by the specified user. This would indicate a surrogate key for the value of you're recipe id (a surrogate is a key that is automatically generated for you, so you're field for recipe id would be an auto_increment int).
Another design approach would be to use a composite key between the recipe id and the user id. They are still unrelated (in that you only want the user id), but are unique and can be used as an easy count between each user. This would have data like so:
recipe id, userid, name
1, 1, 'Cake'
2, 1, 'Cheese Cake'
1, 2, 'Shortbread'
etc

Both are equally usable, though the surrogate approach does provide easier extension into additional tables for you're recipes (the latter approach would require both the recipe id and the user id to lookup a single recipe, while the former would require only the recipe id).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-20-2009, 01:54 AM   PM User | #5
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
should it not be

Code:
select
  pu.name
, pr.recipe_name
from prefix_users pu
inner join
prefix_recipes pr
on pr.user_id = pu.id
table structure should be (I reckon anyway)

table: prefix_users
user_id not null auto_increment primary key

table: prefix_recipes
user_id int not null
name (recipe's name) varchar(99)

bazz
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.

Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
Useful MySQL resource
Useful MySQL link

Last edited by bazz; 02-20-2009 at 01:57 AM..
bazz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:50 AM.


Advertisement
Log in to turn off these ads.