我有一个作业问题:
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.
现在,表“Relationships”具有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时它是如何行为的?