<tfoot id='V2cGq'></tfoot>

    <bdo id='V2cGq'></bdo><ul id='V2cGq'></ul>

    <small id='V2cGq'></small><noframes id='V2cGq'>

      <i id='V2cGq'><tr id='V2cGq'><dt id='V2cGq'><q id='V2cGq'><span id='V2cGq'><b id='V2cGq'><form id='V2cGq'><ins id='V2cGq'></ins><ul id='V2cGq'></ul><sub id='V2cGq'></sub></form><legend id='V2cGq'></legend><bdo id='V2cGq'><pre id='V2cGq'><center id='V2cGq'></center></pre></bdo></b><th id='V2cGq'></th></span></q></dt></tr></i><div id='V2cGq'><tfoot id='V2cGq'></tfoot><dl id='V2cGq'><fieldset id='V2cGq'></fieldset></dl></div>
        <legend id='V2cGq'><style id='V2cGq'><dir id='V2cGq'><q id='V2cGq'></q></dir></style></legend>

        MySQL:选择连接表匹配所有值的记录

        时间:2023-10-09

          <i id='o77J9'><tr id='o77J9'><dt id='o77J9'><q id='o77J9'><span id='o77J9'><b id='o77J9'><form id='o77J9'><ins id='o77J9'></ins><ul id='o77J9'></ul><sub id='o77J9'></sub></form><legend id='o77J9'></legend><bdo id='o77J9'><pre id='o77J9'><center id='o77J9'></center></pre></bdo></b><th id='o77J9'></th></span></q></dt></tr></i><div id='o77J9'><tfoot id='o77J9'></tfoot><dl id='o77J9'><fieldset id='o77J9'></fieldset></dl></div>
          <tfoot id='o77J9'></tfoot>
            <bdo id='o77J9'></bdo><ul id='o77J9'></ul>

              <legend id='o77J9'><style id='o77J9'><dir id='o77J9'><q id='o77J9'></q></dir></style></legend>

              • <small id='o77J9'></small><noframes id='o77J9'>

                    <tbody id='o77J9'></tbody>

                  本文介绍了MySQL:选择连接表匹配所有值的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在努力寻找所有拥有多种技能的员工.以下是表格:

                  I'm trying to find all employees with multiple skills. Here are the tables:

                  CREATE TABLE IF NOT EXISTS `Employee` (
                    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                    `Name` varchar(100) DEFAULT NULL,
                    PRIMARY KEY (`ID`)
                  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
                  INSERT INTO `Employee` (`ID`, `Name`, `Region_ID`) VALUES (1, 'Fred Flintstone'), (2, 'Barney Rubble');
                  
                  CREATE TABLE IF NOT EXISTS `Skill` (
                    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                    `Name` varchar(100) DEFAULT NULL,
                    PRIMARY KEY (`ID`)
                  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
                  INSERT INTO `Skill` (`ID`, `Name`) VALUES (1, 'PHP'), (2, 'JQuery');
                  
                  CREATE TABLE IF NOT EXISTS `Emp_Skills` (
                    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                    `Emp_ID` bigint(20) unsigned NOT NULL DEFAULT '0',
                    `Skill_ID` bigint(20) unsigned NOT NULL DEFAULT '0',
                    PRIMARY KEY (`ID`)
                  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
                  INSERT INTO `Emp_Skills` (`ID`, `Emp_ID`, `Skill_ID`) VALUES (1, 1, 1), (2, 1, 2), (3, 2, 1);
                  

                  这是我目前的查询:

                  SELECT DISTINCT(em.ID), em.Name 
                  FROM Employee em 
                  INNER JOIN Emp_Skills es ON es.Emp_ID = em.ID
                  WHERE es.Skill_ID IN ('1', '2')
                  

                  这会返回两个员工,但是,我需要找到同时拥有这两种技能(ID 1 和 2)的员工.

                  This returns both employees, however, I need to find the employee that has both skills (ID 1 and 2).

                  有什么想法吗?谢谢

                  推荐答案

                  这样做:

                  SELECT EmpId, Name
                  FROM
                  (
                     SELECT em.ID as EmpId, em.Name, es.ID as SkillID 
                     FROM Employee em 
                     INNER JOIN Emp_Skills es ON es.Emp_ID = em.ID
                     WHERE es.Skill_ID IN ('1', '2')
                   ) X
                  GROUP BY EmpID, Name
                  HAVING COUNT(DISTINCT SkillID) = 2;
                  

                  在这里小提琴:

                  区别只是以防同一个员工将技能列出两次.

                  The distinct is just in case the same employee has the skill listed twice.

                  感谢提供测试数据.

                  这篇关于MySQL:选择连接表匹配所有值的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:(+) 在 Oracle SQL 中有什么作用? 下一篇:理解规范化重复 - 我猜我没有 - 添加艺术家和标题 ID

                  相关文章

                • <legend id='SewgZ'><style id='SewgZ'><dir id='SewgZ'><q id='SewgZ'></q></dir></style></legend>
                  • <bdo id='SewgZ'></bdo><ul id='SewgZ'></ul>

                    <i id='SewgZ'><tr id='SewgZ'><dt id='SewgZ'><q id='SewgZ'><span id='SewgZ'><b id='SewgZ'><form id='SewgZ'><ins id='SewgZ'></ins><ul id='SewgZ'></ul><sub id='SewgZ'></sub></form><legend id='SewgZ'></legend><bdo id='SewgZ'><pre id='SewgZ'><center id='SewgZ'></center></pre></bdo></b><th id='SewgZ'></th></span></q></dt></tr></i><div id='SewgZ'><tfoot id='SewgZ'></tfoot><dl id='SewgZ'><fieldset id='SewgZ'></fieldset></dl></div>

                      <small id='SewgZ'></small><noframes id='SewgZ'>

                      <tfoot id='SewgZ'></tfoot>