MySQL 来自相同表的 3 行,它们相互匹配

MySQL 3 rows from the same tables WHERE they match eachother

编辑 - 回应@Strawberry

在@Strawberry 的推荐下,我阅读了有关规范化的内容,它震撼了我的整个布局。下面是我的新布局的模型。它将大大减少长 运行 中的 table 大小。如果你们有任何疑虑,请告诉我到目前为止的任何疑虑。

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

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

我正在开发数独及其变体的数据库。我的问题在于变体。这些变体由 2 个或更多重叠的拼图组成,用于我想出的 2 个拼图。

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

$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 个或更多重叠的谜题。

武士 x3

Puz1 区域 9 = Puz2 区域 1

Puz2 区域 9 = Puz3 区域 1

我不知道如何区分 Puz1 Region9 & Ruz2 Region9 和 Puz2 Region1 & Puz3 Region1

数独9x9

+---------+-------------+------+-----+---------+----------------+
| 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    |                |
+---------+-------------+------+-----+---------+----------------+

区域 1 和区域 9

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

武士3

+-------+---------+------+-----+---------+----------------+
| 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

数独 9x9

+----+-----------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 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(两者 table 相同)

+--------+-----------+-----------+
| 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 拼图和输出,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 相关

根据@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