Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
PHP MYSQL Outer Join?
Old 04-23-2007, 08:17 PM PHP MYSQL Outer Join?
Average Talker

Posts: 29
Name: Paul
Trades: 0
I have to perform a select on a mysql database using php.

here is an example of my tables

Division Table:
ID | Name

Players Table:
ID | Name | TeamID(FK)

Team Table:

ID | Name | DivID(FK)

Ok, here is the problem. I have a previous script I would like to modify. The old script pulled the Team Name from the team table into a dropdown menu and passed the ID variable to the next page where I queried the players by TeamID from the players table and emailed everyone on that team.
What I would like to do now is use the same script and modify it the least amount as possible. I was thinking an outer join, but I don't know exactly how to write it. I now have the drop down menu pulling the Division Name from the Division table and it passes the ID from the Division table. How would I pull the players names by Division when they do not have a DivID in the players table?

Thanks, Paul
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

phlash2k1 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-25-2007, 04:11 AM Re: PHP MYSQL Outer Join?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Either of these should work

SELECT P.name, D.name AS Dname FROM players AS P,team as T LEFT JOIN division AS D ON T.divid = D.id where P.id = [playerID] and T.id = p.teamid ;


SELECT P.name, D.name AS Dname FROM players AS P left join team as T ON T.id = P.teamid JOIN division AS D ON T.divid = D.id where P.id = [playerID] ;
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-25-2007, 06:23 AM Re: PHP MYSQL Outer Join?
Average Talker

Posts: 29
Name: Paul
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Either of these should work

SELECT P.name, D.name AS Dname FROM players AS P,team as T LEFT JOIN division AS D ON T.divid = D.id where P.id = [playerID] and T.id = p.teamid ;


SELECT P.name, D.name AS Dname FROM players AS P left join team as T ON T.id = P.teamid JOIN division AS D ON T.divid = D.id where P.id = [playerID] ;
Ok, I'm a little confused because I do not understand AS i guess. Any good reading on AS? Is this a random name? Is that the variable? Below are my tables:

Code:
CREATE TABLE `cclpV101RC1divisions` (
  `id` int(4) unsigned zerofill NOT NULL auto_increment,
  `name` varchar(50) NOT NULL default '',
  `list_order` int(3) NOT NULL default '1',
  `disable` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
Code:
CREATE TABLE `cclpV101RC1players` (
  `id` int(10) unsigned zerofill NOT NULL auto_increment,
  `lname` varchar(50) NOT NULL default '',
  `fname` varchar(50) NOT NULL default '',
  `birthday` date NOT NULL default '0000-00-00',
  `email` varchar(200) NOT NULL default '',
  `hphone` varchar(20) NOT NULL default '',
  `wphone` varchar(20) NOT NULL default '',
  `address` mediumtext NOT NULL,
  `teamid` int(4) unsigned zerofill NOT NULL default '0000',
  `position` varchar(10) NOT NULL default '',
  `number` int(3) NOT NULL default '0',
  `info` text NOT NULL,
  `photo` varchar(100) NOT NULL default '',
  `registered` int(1) NOT NULL default '0',
  `requested` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=56 ;
Code:
CREATE TABLE `cclpV101RC1teams` (
  `id` int(4) unsigned zerofill NOT NULL auto_increment,
  `name` varchar(50) NOT NULL default '',
  `contact_name` varchar(70) NOT NULL default '',
  `contact_email` varchar(70) NOT NULL default '',
  `hphone` varchar(20) NOT NULL default '',
  `wphone` varchar(20) NOT NULL default '',
  `address` mediumtext NOT NULL,
  `division` varchar(50) NOT NULL default '',
  `password` varchar(50) NOT NULL default '',
  `info` longtext NOT NULL,
  `photo` varchar(100) NOT NULL default '',
  `photo_cap` mediumtext NOT NULL,
  `disabled` int(1) NOT NULL default '1',
  `dstats` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE


Last edited by phlash2k1; 04-25-2007 at 06:31 AM..
phlash2k1 is offline
Reply With Quote
View Public Profile
 
Old 04-25-2007, 07:11 AM Re: PHP MYSQL Outer Join?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
AS simply assigns an alias, so instead of using cclpV101RC1players.name to refer to the name column in the player table.

having cclpV101RC1players AS P allows you to refer to it as P.name

http://www.w3schools.com/sql/sql_alias.asp
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-25-2007, 08:31 AM Re: PHP MYSQL Outer Join?
Average Talker

Posts: 29
Name: Paul
Trades: 0
Quote:
Originally Posted by chrishirst View Post
AS simply assigns an alias, so instead of using cclpV101RC1players.name to refer to the name column in the player table.

having cclpV101RC1players AS P allows you to refer to it as P.name

http://www.w3schools.com/sql/sql_alias.asp
Ok, easy enough. Now to take it a step further...

SELECT P.name, D.name AS Dname FROM players AS P,team as T LEFT JOIN division AS D ON T.divid = D.id where P.id = [playerID] and T.id = p.teamid ;

You are selecting cclpV101RC1players.name and cclpV101RC1divisions.name (grouping them together??) AS (the alias) Dname
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

phlash2k1 is offline
Reply With Quote
View Public Profile
 
Old 04-25-2007, 09:00 AM Re: PHP MYSQL Outer Join?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
You are selecting cclpV101RC1players.name and cclpV101RC1divisions.name (grouping them together??) AS (the alias) Dname
Nope

Only the division name is Dname otherwise you would have two columns in the recordset called "name"

you could have P.name AS Pname, D.name AS Dname
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to PHP MYSQL Outer Join?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23389 seconds with 12 queries