MYSQL选择排列;跨行需要唯一值

时间:2013-01-13 22:12:25

标签: mysql optimization select unique permutation

我想执行一个选择,产生一组排列,而不是组合。换句话说,我想预先形成一个查询,该查询返回表格中各种数量和排列的行。 可以在同一行内水平重复ID

例如我们可以:

9 pick 3 
9 pick 5 
9 pick 7

我有一个有效的查询,但我希望对其进行优化。 WHERE...AND...子句筛选出具有重复ID的行。虽然这有效,但每添加一个选项(列)需要几个额外的AND子句。 对于9 pick 6需要15个!=断言!随着所挑选项目数量的增加,这可能会变得难以处理。它看起来也很臃肿。我正在寻找简单而优雅的东西。

这是我的表:

CREATE TABLE IF NOT EXISTS `table1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `col1` varchar(24) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=30 ;

INSERT INTO `table1` (`id`, `col1`) VALUES
(1, 'Wool Hat'),
(2, 'Wool Gloves'),
(3, 'Fleece Sweater'),
(4, 'Jean Pants'),
(5, 'Cotton T-Shirt'),
(6, 'Wool Socks'),
(7, 'Wool Blanket'),
(8, 'Cotton Boxers'),
(9, 'Fleece Pants');

这是我的疑问:

SELECT * FROM 
`table1` t1, 
`table1` t2, 
`table1` t3,
`table1` t4,
`table1` t5,
`table1` t6

WHERE
t1.id != t2.id
AND
t1.id != t3.id
AND
t1.id != t4.id
AND
t1.id != t5.id
AND
t1.id != t6.id
AND
t2.id != t3.id
AND
t2.id != t4.id
AND
t2.id != t5.id
AND
t2.id != t6.id
AND
t3.id != t4.id 
AND
t3.id != t5.id 
AND
t3.id != t6.id 
AND
t4.id != t5.id
AND
t4.id != t6.id
AND
t5.id != t6.id

ORDER BY t1.col1, t2.col1, t3.col1, t4.col1, t5.col1, t6.col1

感谢您的任何见解!

编辑:

这个例子真是不敬。我只是想以简单有效的方式完成排列的数学概念。

根据要求,这是9 pick 2的预期结果的一半。另一半是切换对。

Wool Hat + Wool Gloves
Wool Hat + Fleece Sweater
Wool Hat + Jean Pants
Wool Hat + Cotton T-Shirt
Wool Hat + Wool Socks
Wool Hat + Wool Blanket
Wool Hat + Cotton Boxers
Wool Hat + Fleece Pants
Wool Gloves + Fleece Sweater
Wool Gloves + Jean Pants
Wool Gloves + Cotton T-Shirt
Wool Gloves + Wool Socks
Wool Gloves + Wool Blanket
Wool Gloves + Cotton Boxers
Wool Gloves + Fleece Pants
Fleece Sweater + Jean Pants
Fleece Sweater + Cotton T-Shirt
Fleece Sweater + Wool Socks
Fleece Sweater + Wool Blanket
Fleece Sweater + Cotton Boxers
Fleece Sweater + Fleece Pants
Jean Pants + Cotton T-Shirt
Jean Pants + Wool Socks
Jean Pants + Wool Blanket
Jean Pants + Cotton Boxers
Jean Pants + Fleece Pants
Cotton T-Shirt + Wool Socks
Cotton T-Shirt + Wool Blanket
Cotton T-Shirt + Cotton Boxers
Cotton T-Shirt + Fleece Pants
Wool Socks + Wool Blanket
Wool Socks + Cotton Boxers
Wool Socks + Fleece Pants
Wool Blanket + Cotton Boxers
Wool Blanket + Fleece Pants

这排除了以下内容:

Wool Hat + Wool Hat
Wool Gloves + Wool Gloves
Fleece Sweater + Fleece Sweater
Jean Pants + Jean Pants
Cotton T-Shirt + Cotton T-Shirt
Wool Socks + Wool Socks
Wool Blanket + Wool Blanket
Cotton Boxers + Cotton Boxers
Fleece Pants + Fleece Pants

1 个答案:

答案 0 :(得分:0)

这是你要找的吗?

SELECT
    t1.`id` AS 'id1',
    t2.`id` AS 'id2',
    t1.`col1` AS 't1_col',
    t2.`col1` AS 't2_col',
    CONCAT(t1.`col1`,' + ',t2.`col1`) AS 'combined'
FROM `table1` t1
CROSS JOIN `table1` t2
WHERE t1.`id` != t2.`id`
-- or
-- WHERE t1.`id` < t2.`id`
ORDER BY t1.`id`,t2.`id`

[编辑] 另一种选择是使用JOINS。以下是9 pick 3的示例。

SELECT
    t1.`id` AS 'id1',
    t2.`id` AS 'id2',
    t3.`id` AS 'id3',
    t1.`col1` AS 't1_col',
    t2.`col1` AS 't2_col',
    t3.`col1` AS 't3_col',
    CONCAT_WS(' + ',t1.`col1`,t2.`col1`,t3.`col1`) AS 'combined'
FROM `table1` t1
INNER JOIN `table1` t2
INNER JOIN `table1` t3
ON
    t2.`id` > t1.`id` AND
    t3.`id` > t2.`id`
ORDER BY t1.`id`,t2.`id`,t3.`id`