我有一个作业问题:
Find the celebs that have been in relationship with the same celeb.
The result should be (celeb1, celeb2, celeb3) triples,
meaning that celeb1 and celeb2 have been in relationship with celeb3.
现在“关系”表有 celeb1 和 celeb2 字段..其中值是 VARCHAR。
我对这个问题的解决方案是:
CREATE VIEW Celeb1Rels AS
SELECT celeb1 AS c1, celeb2 AS c2 FROM relationships;
CREATE VIEW Celeb2Rels AS
SELECT celeb1 AS c2, celeb2 AS c3 FROM relationships;
SELECT * FROM Celeb1Rels NATURAL JOIN Celeb2Rels;
它工作正常。然而,教练发布了他的解决方案,他有:
SELECT X.celeb1, Y.celeb1, X.celeb2
FROM Relationships X, Relationships Y
WHERE X.celeb2=Y.celeb2 AND X.celeb1<Y.celeb1;
我不明白他为什么使用 X.celeb1 < Y.celeb1 它确实有效并给出了正确的输出,但我认为“<”用于比较数字?
谁能解释在这种情况下“<”在做什么?以及在比较 VARCHARS 时它的行为如何?