来自同一个表的MySQL 3行它们彼此匹配

时间:2016-12-27 02:54:03

标签: mysql correlated-subquery

编辑 - 响应@Strawberry

在@ Strawberry的推荐下,我读到了规范化,它震撼了我的整个布局。下面是我的新布局模型。从长远来看,它将大大减少表的大小。如果你们中的任何一个人看到一个,我可以告诉我迄今为止的任何疑虑。

完成布局后,我将编辑我的脚本并运行一些数据。然后尝试沿着不同的路径找出同样的问题。

任何指针总是很好。谢谢@Strawberry的建议。

enter image description here

我正在为数独和变体制作数据库。我的问题在于变体。这些变体由2个或更多重叠的谜题组成,用于2个谜题我想出了如何。

Powershell脚本我用来构建Samurai x2谜题列表

$samurai2 = "SELECT region9.ID, region1.ID FROM region9, region1 WHERE region9.Puz = region1.Puz AND region9.Ans = region1.Ans AND NOT EXISTS ( SELECT 1 FROM samurai2 AS c WHERE c.Puz1 = region9.ID AND c.Puz2 = region1.ID);"

Invoke-MySqlQuery -Query $samurai2 | ForEach {
    $found = "SELECT * FROM sudoku.sudoku9x9 WHERE ID = $($_.'ID') OR ID = $($_.'ID1')"
    $found = (Invoke-MySqlQuery -Query $found)
    $diff = ([int]$found.'diff'[0] + [int]$found.'diff'[1]) / 2

    $samurai = "INSERT INTO ``samurai2``(``Diff``, ``Puz1``, ``Puz2``) VALUES ('$diff', '$($_.'ID')', '$($_.'ID1')')"
    Invoke-MySqlQuery -Query $samurai
}

我试图弄清楚如何找到3个或更多重叠的谜题。

Samurai x3

Puz1 Region9 = Puz2 Region1

Puz2 Region9 = Puz3 Region1

我无法弄清楚如何区分Puz1 Region9& Ruz2 Region9和Puz2 Region1& Puz3 Region1

sudoku9x9

+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| ID      | int(22)     | NO   | PRI | NULL    | auto_increment |
| StringP | varchar(81) | NO   | UNI | NULL    |                |
| StringA | varchar(81) | NO   |     | NULL    |                |
| Diff    | int(6)      | NO   |     | NULL    |                |
| A1P     | int(1)      | YES  |     | NULL    |                |
| A2P     | int(1)      | YES  |     | NULL    |                |
| A3P     | int(1)      | YES  |     | NULL    |                |
| A4P     | int(1)      | YES  |     | NULL    |                |
| A5P     | int(1)      | YES  |     | NULL    |                |
-----------------------------------------------------------------
| I5A     | int(1)      | NO   |     | NULL    |                |
| I6A     | int(1)      | NO   |     | NULL    |                |
| I7A     | int(1)      | NO   |     | NULL    |                |
| I8A     | int(1)      | NO   |     | NULL    |                |
| I9A     | int(1)      | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

region1& region9

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID    | int(11)     | NO   | PRI | NULL    |       |
| Puz   | varchar(81) | NO   | MUL | NULL    |       |
| Ans   | varchar(81) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

samurai3

+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| ID    | int(11) | NO   | PRI | NULL    | auto_increment |
| Diff  | int(6)  | YES  |     | NULL    |                |
| Puz1  | int(11) | YES  |     | NULL    |                |
| Puz2  | int(11) | YES  |     | NULL    |                |
| Puz3  | int(11) | YES  |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+

编辑 - 回应@Gurwinder Singh

sudoku9x9

+----+-----------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| ID | StringP                                                                           | StringA                                                                           | Diff | A1P  | A2P  | A3P  | A4P  | A5P  | A6P  | A7P  | A8P  | A9P  | B1P  | B2P  | B3P  | B4P  | B5P  | B6P  | B7P  | B8P  | B9P  | C1P  | C2P  | C3P  | C4P  | C5P  | C6P  | C7P  | C8P  | C9P  | D1P  | D2P  | D3P  | D4P  | D5P  | D6P  | D7P  | D8P  | D9P  | E1P  | E2P  | E3P  | E4P  | E5P  | E6P  | E7P  | E8P  | E9P  | F1P  | F2P  | F3P  | F4P  | F5P  | F6P  | F7P  | F8P  | F9P  | G1P  | G2P  | G3P  | G4P  | G5P  | G6P  | G7P  | G8P  | G9P  | H1P  | H2P  | H3P  | H4P  | H5P  | H6P  | H7P  | H8P  | H9P  | I1P  | I2P  | I3P  | I4P  | I5P  | I6P  | I7P  | I8P  | I9P  | A1A | A2A | A3A | A4A | A5A | A6A | A7A | A8A | A9A | B1A | B2A | B3A | B4A | B5A | B6A | B7A | B8A | B9A | C1A | C2A | C3A | C4A | C5A | C6A | C7A | C8A | C9A | D1A | D2A | D3A | D4A | D5A | D6A | D7A | D8A | D9A | E1A | E2A | E3A | E4A | E5A | E6A | E7A | E8A | E9A | F1A | F2A | F3A | F4A | F5A | F6A | F7A | F8A | F9A | G1A | G2A | G3A | G4A | G5A | G6A | G7A | G8A | G9A | H1A | H2A | H3A | H4A | H5A | H6A | H7A | H8A | H9A | I1A | I2A | I3A | I4A | I5A | I6A | I7A | I8A | I9A |
+----+-----------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|  1 | 062017009701000000000000008003061000080000000000042006006023007000000000030405010 | 862317549751984263394256178423861795685739421179542836546123987217698354938475612 |  332 | NULL |    6 |    2 | NULL |    1 |    7 | NULL | NULL |    9 |    7 | NULL |    1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |    8 | NULL | NULL |    3 | NULL |    6 |    1 | NULL | NULL | NULL | NULL |    8 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |    4 |    2 | NULL | NULL |    6 | NULL | NULL |    6 | NULL |    2 |    3 | NULL | NULL |    7 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |    3 | NULL |    4 | NULL |    5 | NULL |    1 | NULL |   8 |   6 |   2 |   3 |   1 |   7 |   5 |   4 |   9 |   7 |   5 |   1 |   9 |   8 |   4 |   2 |   6 |   3 |   3 |   9 |   4 |   2 |   5 |   6 |   1 |   7 |   8 |   4 |   2 |   3 |   8 |   6 |   1 |   7 |   9 |   5 |   6 |   8 |   5 |   7 |   3 |   9 |   4 |   2 |   1 |   1 |   7 |   9 |   5 |   4 |   2 |   8 |   3 |   6 |   5 |   4 |   6 |   1 |   2 |   3 |   9 |   8 |   7 |   2 |   1 |   7 |   6 |   9 |   8 |   3 |   5 |   4 |   9 |   3 |   8 |   4 |   7 |   5 |   6 |   1 |   2 |
+----+-----------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

region1& region9(两个表都相同)

+--------+-----------+-----------+
| ID     | Puz       | Ans       |
+--------+-----------+-----------+
| 427974 | 000000000 | 123458976 |
| 169852 | 000000000 | 123459678 |
| 162359 | 000000000 | 123468957 |
| 290352 | 000000000 | 123468975 |
| 418083 | 000000000 | 123469578 |
+--------+-----------+-----------+

samurai2(samurai3还有一列Puz3)

+----+------+-------+-------+
| ID | Diff | Puz1  | Puz2  |
+----+------+-------+-------+
|  1 |  338 | 28005 | 45032 |
|  2 |  261 | 35934 | 60202 |
|  3 |  358 | 49688 | 61760 |
|  4 |  299 | 21243 | 45461 |
|  5 |  360 | 39237 | 56552 |
+----+------+-------+-------+

查询Samurai x2 Puzzles和输出,x3将有第三个ID。

SELECT region9.ID, region1.ID FROM region9, region1 WHERE region9.Puz = region1.Puz AND region9.Ans = region1.Ans AND NOT EXISTS ( SELECT 1 FROM samurai2 AS c WHERE c.Puz1 = region9.ID AND c.Puz2 = region1.ID);
+--------+--------+
| ID     | ID     |
+--------+--------+
| 568557 |  33176 |
|  52551 | 567499 |
| 416691 | 567885 |
| 566193 | 472617 |
| 563401 | 567324 |
+--------+--------+

sudoku9x9中使用的ID与region1和region9中的ID

相关联

1 个答案:

答案 0 :(得分:0)

根据@ Strawberry的评论调查正常化后。我重新编写了整个数据库,看到我对问题的最后编辑。使用新架构,我的原始问题很容易解决。

SELECT puz1.sudoku9x9_id, puz1.difficulty, puz2.sudoku9x9_id, puz2.difficulty, puz3.sudoku9x9_id, puz3.difficulty
    FROM sudoku9x9 as puz1
    INNER JOIN sudoku9x9 as puz2
    INNER JOIN sudoku9x9 as puz3
    WHERE 
        puz1.clue_9 = puz2.clue_1 AND puz1.region_9 = puz2.region_1 AND
        puz2.clue_9 = puz3.clue_1 AND puz2.region_9 = puz3.region_1