回複:超級SQL STATEMENT問題(請求天才)....

來源: 2010-02-10 22:09:58 [舊帖] [給我悄悄話] 本文已被閱讀:

Not quite difficult. Many people can do it. Here is one of the solutions:

(use TEST_TABLE as the table mentioned in the question)

SELECT DISTINCT t1.`NAME` FROM TEST_TABLE t1
WHERE t1.`NAME` != 'alex'
AND
( SELECT COUNT(DISTINCT t2.GADGET) FROM TEST_TABLE t2
WHERE t2.NAME = 'alex'
AND t2.GADGET IN (
SELECT DISTINCT t3.GADGET FROM TEST_TABLE t3
WHERE t3.`NAME` = t1.`NAME`
)) = ( SELECT COUNT(DISTINCT t4.GADGET) FROM TEST_TABLE t4
WHERE t4.NAME = 'alex')
;