2012-07-15 29 views
0

需要一些幫助 我在數據庫中有5張桌子,這裏是MySQL代碼:我如何使用mysql和php匹配數據?

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; 

我想使用他們的技能匹配候選人和工作,並且我有成功的類型,但是即使他們只有一項技能並且工作具有三種技能,即使候選人與工作匹配。 有人可以請指點我在正確的方向,因爲我已經搜索,測試並記錄了幾天但我無法移過這個問題

回答

0

你正在做一個簡單的加入,所以任何候選人誰甚至只有一個上市技能將相匹配。您需要實際計算匹配多少所需技能,並使用該技能過濾查詢結果。

沒有rejiggering你的整個查詢,它會是這樣的:

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

This'd限制有需要的技能,究竟誰3候選人。如果你想「至少2」或類似的類型,那麼它會是

HAVING (cnt >= 2) 

等等其他變化。

+0

謝謝你兄弟,你救了我在那裏: – 2012-07-15 08:38:55

+0

你能讓我知道我可以如何重組整個查詢嗎? – 2012-07-16 02:51:07