Re: [PHP-DB] One to Many Select Statement
John Holmes wrote: Gerard Samuel wrote: Looking for clarification... Say that I have a user table and group table -> -- user table -- id username 1 foo -- group table -- id user_id group_name 1 1group_1 2 1group_2 Would this be the proper way to construct a select statement for this -> select u.username, g.group_name from user u, group g where u.user.id = g.user_id and user.id = 1; The results should look like this -> username group_name foo group 1 foo group 2 What I would like to clarify, is if this is the proper (only) way to construct a one to many select statement? I've always thought I was doing something illegal, since username is displayed more than once, and I would like to shake that thought off my back. Other than using "u.user.id" and "user.id" instead of "u.id" (just typos, I'm sure), Man, and I proof read the message before I sent it out. ;) there's nothing really wrong with that query. Good. You can do that join this way, though, which may be more "proper": SELECT u.username, g.group_name FROM user u JOIN group g ON u.id = g.id WHERE u.id = 1 Thanks for the tip... -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] One to Many Select Statement
Gerard Samuel wrote: Looking for clarification... Say that I have a user table and group table -> -- user table -- id username 1 foo -- group table -- id user_id group_name 1 1group_1 2 1group_2 Would this be the proper way to construct a select statement for this -> select u.username, g.group_name from user u, group g where u.user.id = g.user_id and user.id = 1; The results should look like this -> username group_name foo group 1 foo group 2 What I would like to clarify, is if this is the proper (only) way to construct a one to many select statement? I've always thought I was doing something illegal, since username is displayed more than once, and I would like to shake that thought off my back. Other than using "u.user.id" and "user.id" instead of "u.id" (just typos, I'm sure), there's nothing really wrong with that query. You can do that join this way, though, which may be more "proper": SELECT u.username, g.group_name FROM user u JOIN group g ON u.id = g.id WHERE u.id = 1 oh, and -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] One to Many Select Statement
Looking for clarification... Say that I have a user table and group table -> -- user table -- id username 1 foo -- group table -- id user_id group_name 1 1group_1 2 1group_2 Would this be the proper way to construct a select statement for this -> select u.username, g.group_name from user u, group g where u.user.id = g.user_id and user.id = 1; The results should look like this -> username group_name foo group 1 foo group 2 What I would like to clarify, is if this is the proper (only) way to construct a one to many select statement? I've always thought I was doing something illegal, since username is displayed more than once, and I would like to shake that thought off my back. Thanks for your input.. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php