有人可以通过工作示例/示例向我解释以下声明。提前致谢。您不能创建重复的字段,而只需添加一个额外的字段“ coupleId”,该字段将为每对夫妇提供唯一的ID;每对夫妇两排(每人一排);然后使用a.coupleId = b.coupleId和a.id b.id这样的约束将表与自身联接,以便可以将数据压缩为给定对的单个结果行。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 给定表Person,如下所示:Person==========PersonID (int, autoincrement)CoupleID (nullable int)Name (nullable varchar(50))插入一些行:insert into Person (CoupleID, Name) values (1, 'John')insert into Person (CoupleID, Name) values (1, 'Mary')insert into Person (CoupleID, Name) values (null, 'Sue')现在,您可以通过以下查询找到组成一对的人:select p1.CoupleID, p1.PersonID as Person1ID, p1.Name as Person1Name, p2.PersonID as Person2ID, p2.Name as Person2Namefrom Person p1inner join Person p2 on p1.CoupleID = p2.CoupleIDwhere p1.PersonID <> p2.PersonIDorder by p1.PersonID输出:PersonID CoupleID Name PersonID CoupleID Name----------- ----------- -------------------------------------------------- ----------- ----------- --------------------------------------------------2 1 Mary 1 1 John1 1 John 2 1 Mary(2 row(s) affected) (adsbygoogle = window.adsbygoogle || []).push({});
07-25 22:57