如何在mysql上的四个表之间加入

时间:2014-05-29 15:56:19

标签: mysql join

我有4个表用于mysql数据库,我如何加入他们之间

用户:

userid|username
   1  | mark
   2  | jon

awards_user:

awardid|userid
 1     |1    
 2     |2    
猫:

catid|catname
   1 | english
   2 | computer
奖励:

awardid|catid|awardname|awardlink
 1     |1    |best1    |pic link
 2     |2    |best2    |pic link

结果:

用户ID |用户名| CATID | catname | awardid | awardname | awardlink

好的,这是我的尝试:

有效,但当成员持有奖励时,只显示一个结果 ,我想展示所有的奖项,即使没有人持有它们。

$all_awards = $db->query_read(" SELECT * FROM " . TABLE_PREFIX . " users,awards_user,cat,awards
 WHERE
awards_user.awardid = awards.awardid
 AND
awards_user.userid = users.userid
 AND
awards.catid = cat.catid
 ");


CREATE TABLE `awards` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `forumid` int(10) unsigned NOT NULL,
  `name` varchar(100) NOT NULL default '',
  `link` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

-- 
-- Dumping data for table `awards`
-- 

INSERT INTO `awards` VALUES (1, 2, 'award one', 'http://www.mwadah.com/pict/noway9.gif');
INSERT INTO `awards` VALUES (2, 1, 'award 2', 'http://www.forum-ksa.com/up/uploads/images/forum-ksac59a7122d1.gif');

-- --------------------------------------------------------

-- 
-- Table structure for table `awards_user`
-- 

CREATE TABLE `awards_user` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `awardid` int(10) unsigned NOT NULL,
  `userid` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

-- 
-- Dumping data for table `awards_user`
-- 

INSERT INTO `awards_user` VALUES (2, 2, 2);

-- --------------------------------------------------------

-- 
-- Table structure for table `forum`
-- 

CREATE TABLE `forum` (
  `forumid` smallint(5) unsigned NOT NULL auto_increment,
  `title` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`forumid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2;

-- 
-- Dumping data for table `forum`
-- 

INSERT INTO `forum` VALUES (1,'Main Category');
INSERT INTO `forum` VALUES (2,'Main Forum');
-- --------------------------------------------------------

-- 
-- Table structure for table `user`
-- 

CREATE TABLE `user` (
  `userid` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(100) NOT NULL default ''
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

-- 
-- Dumping data for table `user`
-- 

INSERT INTO `user` VALUES (1,'admin');
INSERT INTO `user` VALUES (1,'mark');

2 个答案:

答案 0 :(得分:0)

您可以像这样构建JOIN查询:

SELECT *
FROM users
JOIN awards_user USING(userid)
JOIN awards USING(awardid)
JOIN cat USING(catid)

然后,您可以添加WHERE子句来过滤结果。

答案 1 :(得分:0)

SELECT awards.userid, 
       username, 
       awards.catid, 
       catname, 
       awards.awardid, 
       awardname, 
       awardlink 
FROM   users, 
       awards_user, 
       cat, 
       awards 
WHERE  awards_user.userid = users.userid 
       AND awards_user.awardid = awards.awardid 
       AND cat.catid = awards.catid;