多个带有INNER JOIN不同参数的SQL查询?

时间:2019-06-19 12:53:01

标签: mysql select inner-join

我不确定该用什么术语,但是我有一个mysql查询

SELECT name, invTypes.typeName, character_skills.trained_skill_level FROM character_skills INNER JOIN character_infos ON character_infos.character_id = character_skills.character_id INNER JOIN invTypes ON character_skills.skill_id = invTypes.typeID WHERE character_skills.active_skill_level <5 AND skill_id = 12203 AND name = 'Mokey PYD';

但是,有2个变量会发生变化,我需要在一个表上显示所有这些变量。

这两个变量是

WHERE character_skills.active_skill_level <5 AND skill_id = 12203

例如

我需要查看active_skill_level is < 5的{​​{1}}的值,但还需要检查skill_id 12203的{​​{1}}是否也要显示在同一张表上。 ..

有人可以帮我一点忙吗?非常感谢。

2 个答案:

答案 0 :(得分:4)

使用UNION ALL

SELECT name, invTypes.typeName, character_skills.trained_skill_level 
FROM character_skills INNER JOIN character_infos ON character_infos.character_id = character_skills.character_id 
INNER JOIN invTypes ON character_skills.skill_id = invTypes.typeID 
WHERE character_skills.active_skill_level <5 AND skill_id = 12203 AND name = 'Mokey PYD'
union all
SELECT name, invTypes.typeName, character_skills.trained_skill_level 
FROM character_skills INNER JOIN character_infos ON character_infos.character_id = character_skills.character_id 
INNER JOIN invTypes ON character_skills.skill_id = invTypes.typeID 
WHERE character_skills.active_skill_level <4 AND skill_id = 12204 AND name = 'Mokey PYD'

答案 1 :(得分:3)

联合所有条件的替代方法是OR子句

SELECT name, invTypes.typeName
  , character_skills.trained_skill_level 
FROM character_skills 
INNER JOIN character_infos ON character_infos.character_id = character_skills.character_id 
INNER JOIN invTypes ON character_skills.skill_id = invTypes.typeID 
WHERE ( character_skills.active_skill_level <5 
AND skill_id = 12203  )  
OR  ( character_skills.active_skill_level <4 
AND skill_id = 12204  ) 
AND name = 'Mokey PYD';