DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   MySQL (http://www.daniweb.com/forums/forum126.html)
-   -   query (http://www.daniweb.com/forums/thread120194.html)

queenc Apr 21st, 2008 5:49 am
query
 
hi
i have two tables,i want to display the details in startpost and based on the userid i want to display the username
ie i want to print
posttitle,date,name
using single query please help
CREATE TABLE `creuser` (
  `uid` int(11) NOT NULL auto_increment,
  `username` varchar(100) NOT NULL,
    UNIQUE KEY `uid` (`uid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

CREATE TABLE `startpost` (
    `posttitle` varchar(300) NOT NULL,
  `userid` int(50) NOT NULL,
  `dat` datetime NOT NULL,
  PRIMARY KEY  (`postid`),
  UNIQUE KEY `postid` (`postid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;

Fungus1487 Apr 21st, 2008 6:32 am
Re: query
 
SELECT startpost.posttitle, startpost.dat, creuser.username FROM startpost INNER JOIN creuser ON startpost.userid = creuser.uid
That will display all the startpost data with the username for the startposts. Specifying a Where clause like below will let you show one startpost record based on its id.
SELECT startpost.posttitle, startpost.dat, creuser.username FROM startpost INNER JOIN creuser ON startpost.userid = creuser.uid WHERE startpost.postid = 1

This is really quite basic and i suggest you read some articles explaining "JOINS" a good one is here w3schools

queenc Apr 21st, 2008 7:01 am
Re: query
 
thanks a lot


All times are GMT -4. The time now is 11:18 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC