SQL加入初学者

时间:2017-07-17 12:13:17

标签: sql join

我对sql很新,我正在努力创建一个查询。我有两张桌子:

字符

  • 或CharacterId
  • 姓名
  • 名字
  • 性别

夫妻

  • coupleID
  • femaleID
  • maleID

现在我试图把它拼凑起来:

  • femaleFName
  • femaleLName
  • maleFName
  • maleLName

我试过了:

var dbCofing = {
    user: 'USER',
    password: 'PASS',
    server: '53.165.137.28\\NAME,94273',
    database: 'DATABASE'
}

但现在我正在努力与第二次加入。

3 个答案:

答案 0 :(得分:1)

您所要做的就是第二次加入桌子。为此,您必须提供别名,以便不会混淆表格。

SELECT 
cf.firstname AS femaleFName, cf.lastname AS femaleLName,
mf.firstname AS maleFName, mf.lastname AS maleLName
FROM Couple 
INNER JOIN Character AS cf ON cf.characterID = Couple.femaleID
INNER JOIN Character AS mf ON mf.characterID = Couple.maleID

关键字AS不是强制性的,可以省略。

答案 1 :(得分:0)

添加另一个JOIN,如下所示。这里c是区分第二个连接的表别名

SELECT character.firstname AS femaleFName, character.lastname AS femaleLName
FROM character INNER JOIN Couple ON Character.characterID = Couple.femaleID
INNER JOIN Couple c ON Character.characterID = c.maleID

答案 2 :(得分:0)

您需要使用自我加入来获得所需的结果。使用以下查询可以根据需要获得结果。

WARNINGS:
?: (urls.W002) Your URL pattern '^/(?P<slug>[\w-]+)/form/$'
[name='person_form'] has a regex beginning with a '/'. Remove this 
slash as it is unnecessary. If this pattern is targeted in an 
include(), ensure the include() pattern has a trailing '/'.

我希望这对你有所帮助。

谢谢!