加入两个表并显示不同的列列表

时间:2013-08-29 09:09:56

标签: php mysql database

我有三张桌子。

  1. 候选人
  2. enter image description here

    1. 技能

      enter image description here

    2. CandidateToSkillMap

      enter image description here

    3. 候选技能表格映射在 CandidateToSkillMap 表格中。在那个candidate_id和skill_id是外键。

      如何使用它们获得以下输出?

      假设我需要显示candidate_id = 1

      的计算机技能
        

      电脑技能 -

           
          
      • MS Word(x)
      •   
      • MS Excel(x)
      •   
      • MS Paint()
      •   

      它应该显示所有可用计算机技能的清晰列表,并且应该在前面有一个复选框。将检查特定候选人是否具备该技能。

      编辑 -

      我有查询数据库并将数据加载到数组中 -

      $ candidate_profile_computer_skills

      此数组提供所有计算机技能

      array (size=3)
        0 => 
          object(Candidate\Model\CandidateProfileSkill)[317]
            public 'id' => string '1' (length=1)
            public 'skill_name' => string 'MS Word' (length=7)
            public 'candidate_id' => null
            public 'candidateprofileskill_id' => null
        1 => 
          object(Candidate\Model\CandidateProfileSkill)[223]
            public 'id' => string '3' (length=1)
            public 'skill_name' => string 'MS Excel' (length=8)
            public 'candidate_id' => null
            public 'candidateprofileskill_id' => null
        2 => 
          object(Candidate\Model\CandidateProfileSkill)[316]
            public 'id' => string '6' (length=1)
            public 'skill_name' => string 'MS Paint' (length=8)
            public 'candidate_id' => null
            public 'candidateprofileskill_id' => null
      

      $ candidate_profile_skills_map

      此数组返回Skills和CandidateToSkillMap,其中LEFT JOIN为特定候选人(candidate_id = 1)

      array (size=5)
        0 => 
          object(Candidate\Model\CandidateProfileSkill)[321]
            public 'id' => string '1' (length=1)
            public 'skill_name' => string 'MS Word' (length=7)
            public 'candidate_id' => string '1' (length=1)
            public 'candidateprofileskill_id' => string '1' (length=1)
        1 => 
          object(Candidate\Model\CandidateProfileSkill)[322]
            public 'id' => string '2' (length=1)
            public 'skill_name' => string 'Sinhala Reading' (length=15)
            public 'candidate_id' => string '1' (length=1)
            public 'candidateprofileskill_id' => string '2' (length=1)
        2 => 
          object(Candidate\Model\CandidateProfileSkill)[323]
            public 'id' => string '3' (length=1)
            public 'skill_name' => string 'MS Excel' (length=8)
            public 'candidate_id' => string '1' (length=1)
            public 'candidateprofileskill_id' => string '3' (length=1)
        3 => 
          object(Candidate\Model\CandidateProfileSkill)[324]
            public 'id' => string '4' (length=1)
            public 'skill_name' => string 'English Reading' (length=15)
            public 'candidate_id' => string '1' (length=1)
            public 'candidateprofileskill_id' => string '4' (length=1)
        4 => 
          object(Candidate\Model\CandidateProfileSkill)[325]
            public 'id' => string '4' (length=1)
            public 'skill_name' => string 'English Reading' (length=15)
            public 'candidate_id' => string '1' (length=1)
            public 'candidateprofileskill_id' => string '4' (length=1)
      

5 个答案:

答案 0 :(得分:0)

联接将执行:

SELECT
    s.skill_name,
    CASE WHEN c.candidate_id IS NOT NULL THEN '(X)' ELSE '()' END has_skill
FROM
    skills s
    LEFT JOIN CandidateToSkillMap c ON s.skill_id = c.skill_id
WHERE
    c.candidate_id = 1

如果它成功加入右侧,因此c.candidate_id将不为空,那么这意味着该人具有该技能并且您将其标记为其他 - 请不要。

答案 1 :(得分:0)

你可以使用这个

Select S.SkillName, count(*) from CandidateToSkillMap CTS, Skills S where S.Id = CTS.CandidateProfileSkillId group by CV.Id having CTS.CandidateId = 1

答案 2 :(得分:0)

SELECT * FROM [Skills] s
LEFT JOIN [CandidateToSkillMap] csm ON csm.candidateprofileskill_id = s.id
WHERE csm.candidate_id = 1

这将返回技能表中的所有行,您可以对“candidate_id”列进行空值检查,以确定相关候选人是否具有该行的技能。

这是一些伪的:

for($row in $result) { 
    Skill name: $row["skill_name"]
    Has skill?: $row["candidate_id"] != null ? "Yes" : "No"
}

答案 3 :(得分:0)

你可以试试这个

select S.skill_name from CandidateToSkillMap CSM, Skills s where  S.id=CSM.candidatesprofileskill_id and CSM.candidate_id=1

答案 4 :(得分:0)

尝试类似

的内容
SELECT s.skill_name, (cm.Candidate_id IS NOT NULL) AS HasSkill

FROM Skills AS s
LEFT OUTER JOIN candidateToSkillMap AS cm
ON s.id = cm.candidateprofileskill_id
WHERE s.skill_type = 'Computer Literacy'
AND (Candidate_id = 1 OR Candidate_id IS NULL)

以上查询使用LEFT OUTER JOIN加入skillscandidatestoskillmap包含技能表中的所有行。这将确保candidatestoskillmap表中不匹配的任何行都包含为空值。

然后select部分查找这些值并为其指定true / false。

Where部分会过滤技能类型和候选人的ID。