studgate Posted March 16, 2008 Share Posted March 16, 2008 Hi Guys, I am trying to get some help with a query that I am working on. I have two tables, posted here: CREATE TABLE `sessions` ( `sessionsid` int(11) NOT NULL auto_increment, `sessionsname` varchar(255) NOT NULL default '', `sport` int(11) NOT NULL default '1', KEY `confid` (`sessionsid`) ); & CREATE TABLE `sports` ( `sportsid` int(11) NOT NULL auto_increment, `sportsname` varchar(50) default NULL, PRIMARY KEY (`sportsid`) ); What I want to do is to get a list of all the items in table one(sessions) which has sessions.sport = sports.sportsid. Thank you in advance. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2008 Share Posted March 16, 2008 Your question is not making sense as posed. You have to have some value that you are comparing against which would lead to a simple query such as: SELECT * FROM `sessions` WHERE `sport` = $somevalue If you are wanting to JOIN the two tables then what is the supplied value you are going to search on - sportsname? In that case the query would look like this: SELECT * FROM `sessions` JOIN `sports` ON sessions.sport = sports.sportsid WHERE `sportname` = $searchvalue However, I would recommend changing the value of `sport` to `sportid` in the `sessions` table, because that is what it is. makes it a lot easier to coordinate the tables. Quote Link to comment Share on other sites More sharing options...
studgate Posted March 16, 2008 Author Share Posted March 16, 2008 the tables are joined together, when I add an entry to the sessions tables, the sportsid of the second one is added in the sport table. And I want to be able to get a list of all the data in sessions table that match the second table id Quote Link to comment Share on other sites More sharing options...
Barand Posted March 17, 2008 Share Posted March 17, 2008 SELECT sessions.* FROM `sessions` JOIN `sports` ON sessions.sport = sports.sportsid Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.