除了“<>”之外,您的查询都很好(事实上,它甚至可能比提出的替代方案快一点!)。只是您的索引需要一些工作...
CREATE TABLE contact
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,firstname VARCHAR(255) NOT NULL
,lastname VARCHAR(255) NOT NULL
,type ENUM('small','medium','large') NOT NULL
);
INSERT INTO contact VALUES
(NULL,'John','Brown','small'),
(NULL,'Bill','Red','small'),
(NULL,'Paul','Orange','medium'),
(NULL,'Mike','Green','large'),
(NULL,'John','Scarlet','small'),
(NULL,'John','Cyan','medium'),
(NULL,'Fiona','Brown','large'),
(NULL,'John','Brown','small'),
(NULL,'Chris','Copper','medium'),
(NULL,'Steve','Silver','large');
INSERT INTO contact SELECT NULL,x.firstname, y.lastname, z.type FROM contact x, contact y, contact z;
SELECT COUNT(*) FROM contact;
+----------+
| COUNT(*) |
+----------+
| 1010 |
+----------+
1 row in set (0.01 sec)
SELECT c1.id c1_id
, c2.id c2_id
FROM contact c1
JOIN contact c2
ON c1.firstname = c2.firstname
AND c1.lastname = c2.lastname
AND c1.type=c2.type
WHERE c1.id < c2.id;
...
...
| 1006 | 1008 |
+-------+-------+
5634 rows in set (0.16 sec)
所以,现在让我们在 (firstname,lastname,type) 上添加一个索引...
DROP TABLE IF EXISTS contact;
CREATE TABLE contact
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,firstname VARCHAR(255) NOT NULL
,lastname VARCHAR(255) NOT NULL
,type ENUM('small','medium','large') NOT NULL
,INDEX(firstname,lastname,type)
);
INSERT INTO contact VALUES
(NULL,'John','Brown','small'),
(NULL,'Bill','Red','small'),
(NULL,'Paul','Orange','medium'),
(NULL,'Mike','Green','large'),
(NULL,'John','Scarlet','small'),
(NULL,'John','Cyan','medium'),
(NULL,'Fiona','Brown','large'),
(NULL,'John','Brown','small'),
(NULL,'Chris','Copper','medium'),
(NULL,'Steve','Silver','large');
INSERT INTO contact SELECT NULL,x.firstname, y.lastname, z.type FROM contact x, contact y, contact z;
SELECT c1.id c1_id
, c2.id c2_id
FROM contact c1
JOIN contact c2
ON c1.firstname = c2.firstname
AND c1.lastname = c2.lastname
AND c1.type = c2.type
AND c1.id < c2.id;
| 775 | 776 |
...
| 1006 | 1008 |
+-------+-------+
5634 rows in set (0.05 sec)