我如何使用mysql和php匹配数据?

时间:2012-07-15 04:56:09

标签: php mysql

需要一些帮助 我在数据库中有5个表,这里是mysql代码:

CREATE TABLE IF NOT EXISTS `candidate` (
  `CID` int(4) NOT NULL AUTO_INCREMENT,
  `title` varchar(5) NOT NULL,
  `fname` varchar(30) NOT NULL,
  `lname` varchar(30) NOT NULL,
  `dob` date NOT NULL,
  `email` varchar(50) NOT NULL,
  `address` varchar(255) NOT NULL,
  `city` varchar(50) NOT NULL,
  `postcode` varchar(10) NOT NULL,
  `phone_num` varchar(11) NOT NULL,
  `username` varchar(40) NOT NULL,
  `password` varchar(40) NOT NULL,
  `regdate` datetime NOT NULL,
  `acc_type` enum('c','s') NOT NULL DEFAULT 'c',
  `emailactivate` enum('0','1') NOT NULL DEFAULT '0',
  `cv_name` varchar(60) NOT NULL,
  `cv` blob NOT NULL,
  PRIMARY KEY (`CID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='// this is the table for the candidates' AUTO_INCREMENT=175 ;

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

--
-- Table structure for table `candidate_skill`
--

CREATE TABLE IF NOT EXISTS `candidate_skill` (
  `CSID` int(4) NOT NULL AUTO_INCREMENT,
  `CID` int(4) NOT NULL,
  `S_CODE` int(4) NOT NULL,
  PRIMARY KEY (`CSID`),
  KEY `CID` (`CID`),
  KEY `S_CODE` (`S_CODE`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='//match candidate and skill' AUTO_INCREMENT=102 ;

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

--
-- Table structure for table `job`
--

CREATE TABLE IF NOT EXISTS `job` (
  `JID` int(4) NOT NULL AUTO_INCREMENT,
  `job_title` varchar(40) NOT NULL,
  `job_desc` varchar(255) NOT NULL,
  `start_date` date NOT NULL,
  `end_date` date NOT NULL,
  PRIMARY KEY (`JID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='// this is the table for the job vacancies' AUTO_INCREMENT=10 ;

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

--
-- Table structure for table `skill`
--

CREATE TABLE IF NOT EXISTS `skill` (
  `S_CODE` int(4) NOT NULL AUTO_INCREMENT COMMENT '// this is the skill primary key',
  `skill_name` varchar(40) NOT NULL,
  `skill_desc` varchar(255) NOT NULL,
  PRIMARY KEY (`S_CODE`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

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

--
-- Table structure for table `skill_job`
--

CREATE TABLE IF NOT EXISTS `skill_job` (
  `SJID` int(4) NOT NULL AUTO_INCREMENT,
  `JID` int(4) NOT NULL,
  `S_CODE` int(4) NOT NULL,
  PRIMARY KEY (`SJID`),
  KEY `S_CODE` (`S_CODE`),
  KEY `JID` (`JID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=94 ;

这是给我问题的查询:

$query = ("SELECT * FROM candidate, candidate_skill, job, skill, skill_job WHERE candidate_skill.S_CODE=skill_job.S_CODE AND skill.S_CODE=candidate_skill.S_CODE AND candidate.CID=candidate_skill.CID AND job.JID = skill_job.JID");

$result = mysql_query ($query);
$candidate_id ='';      
$canskill ='';
        while ($result = mysql_fetch_array ($res)){
            $candidate_id .= $r['CID'];
            $canskill .= $r ['S_CODE'];


            }
        }


        }
    echo $candidate_id;

我正在尝试使用他们的技能来匹配候选人和工作,并且我有一些成功,但即使他们只有一项技能并且工作具有例如三种技能,我也有候选人匹配工作。 有人可以指出我正确的方向,因为我已经搜索,测试和记录了几天,但我没有超越这个问题

1 个答案:

答案 0 :(得分:0)

你正在做一个简单的加入,所以任何只有一个列出的技能的候选人都会被匹配。您需要实际计算匹配所需技能的数量,并使用该技能过滤查询结果。

如果不重新调整整个查询,它就像是:

SELECT ..., COUNT(skill_job.id) AS cnt
FROM ....
WHERE ...
HAVING (cnt = 3)

这限制了具有3项所需技能的考生。如果你想要“至少2”或类似的类型,那么它就是

HAVING (cnt >= 2)

以及其他变体。