PHP MySQL找到最佳匹配

时间:2015-01-07 18:51:03

标签: php mysql

嗨,抱歉我的英语一开始。 所以我有这样的表

--
-- Table structure for table `peoples`
--

CREATE TABLE IF NOT EXISTS `peoples` (
`id_people` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `peoples`
--

INSERT INTO `peoples` (`id_people`, `name`) VALUES
(1, 'Igor');

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

--
-- Table structure for table `peoples_skills`
--

CREATE TABLE IF NOT EXISTS `peoples_skills` (
  `id_people` int(11) DEFAULT NULL,
  `id_skill` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `peoples_skills`
--

INSERT INTO `peoples_skills` (`id_people`, `id_skill`) VALUES
(1, 1),
(1, 2),
(1, 3);

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

--
-- Table structure for table `skills`
--

CREATE TABLE IF NOT EXISTS `skills` (
`id_skills` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `skills`
--

INSERT INTO `skills` (`id_skills`, `name`) VALUES
(1, 'Analytical'),
(2, 'Accuracy'),
(3, 'Coaching'),
(4, 'Decision making ');

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

--
-- Table structure for table `weapons`
--

CREATE TABLE IF NOT EXISTS `weapons` (
`id_weapon` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `weapons`
--

INSERT INTO `weapons` (`id_weapon`, `name`) VALUES
(1, 'weapon 1'),
(2, 'weapon 2');

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

--
-- Table structure for table `weapons_skills`
--

CREATE TABLE IF NOT EXISTS `weapons_skills` (
  `id_weapon` int(11) DEFAULT NULL,
  `id_skill` int(11) DEFAULT NULL COMMENT '// skill required to use reapon'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `weapons_skills`
--

INSERT INTO `weapons_skills` (`id_weapon`, `id_skill`) VALUES
(1, 1),
(2, 1),
(2, 3);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `peoples`
--
ALTER TABLE `peoples`
 ADD PRIMARY KEY (`id_people`);

--
-- Indexes for table `peoples_skills`
--
ALTER TABLE `peoples_skills`
 ADD KEY `peoples_skills-id_people_idx` (`id_people`), ADD KEY `peoples_skills-id_skil_idx` (`id_skill`);

--
-- Indexes for table `skills`
--
ALTER TABLE `skills`
 ADD PRIMARY KEY (`id_skills`);

--
-- Indexes for table `weapons`
--
ALTER TABLE `weapons`
 ADD PRIMARY KEY (`id_weapon`);

--
-- Indexes for table `weapons_skills`
--
ALTER TABLE `weapons_skills`
 ADD KEY `weapons_skills-id_skill_idx` (`id_skill`), ADD KEY `weapons_skills-id_weapon` (`id_weapon`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `peoples`
--
ALTER TABLE `peoples`
MODIFY `id_people` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
--
-- AUTO_INCREMENT for table `skills`
--
ALTER TABLE `skills`
MODIFY `id_skills` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `weapons`
--
ALTER TABLE `weapons`
MODIFY `id_weapon` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `peoples_skills`
--
ALTER TABLE `peoples_skills`
ADD CONSTRAINT `peoples_skills-id_people` FOREIGN KEY (`id_people`) REFERENCES `peoples` (`id_people`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `peoples_skills-id_skil` FOREIGN KEY (`id_skill`) REFERENCES `skills` (`id_skills`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `weapons_skills`
--
ALTER TABLE `weapons_skills`
ADD CONSTRAINT `weapons_skills-id_weapon` FOREIGN KEY (`id_weapon`) REFERENCES `weapons` (`id_weapon`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `weapons_skills-id_skill` FOREIGN KEY (`id_skill`) REFERENCES `skills` (`id_skills`) ON DELETE NO ACTION ON UPDATE NO ACTION;

你可以看到我有桌子人民,武器,技能和链接表people_skills,weapon_skills(关系多对多),我需要通过检查匹配技能找到最佳匹配武器,如果可能的百分比,如果武器有所有技能,如人将100%,没有相同的技能,它将是0%等,它可能在一些查询中得到它?还是用PHP?我需要找到好的方法,或者数据库的其他结构?但我需要有多对多的关系

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为这将完成这项工作:

select 
p.*, w.*,
COUNT(DISTINCT(ps.id_skill)) as total_skills, 
SUM(IF(ws.id_skill=ps.id_skill, 1, 0)) as matching_skills, 
SUM(IF(ws.id_skill=ps.id_skill, 1, 0)) / COUNT(DISTINCT(ps.id_skill)) * 100 as matching_percentage  
from peoples_skills ps
CROSS JOIN weapons_skills ws
JOIN peoples p ON p.id_people=ps.id_people
JOIN weapons w ON w.id_weapon=ws.id_weapon
GROUP BY p.id_people,w.id_weapon
ORDER BY matching_percentage DESC

matching_percentage是您想要的值。结果从最佳匹配到最差匹配。