0

我有以下表格:

user {user_id, name}
family {child_id, mother_id, father_id, address, household_income}

我想在family给定中选择一行child_id, mother_id and father_id并让它返回所有列family以及与关联的用户名称的 3 个新列child_id, mother_id and father_id

family {child_id, mother_id, father_id, address,
        household_income, child_name, mother_name, father_name}

这可能吗?我最初在家庭表中拥有名称和 id,但我觉得在两个表中重复列是多余的。

4

1 回答 1

1

了解SQL 连接

SELECT family.*,
       child.name  AS  child_name,
       mother.name AS mother_name,
       father.name AS father_name
FROM   family
  JOIN user AS child  ON  child.user_id = family.child_id
  JOIN user AS mother ON mother.user_id = family.mother_id
  JOIN user AS father ON father.user_id = family.father_id
WHERE  family.child_id  = ?
   AND family.mother_id = ?
   AND family.father_id = ?
于 2012-09-05T22:25:13.393 回答