MySQL 3 rows from the same tables WHERE they match eachother

77 views Asked by At

Edit - In Response to @Strawberry

On @Strawberry's recommendation I read up on normalization and it rocked my entire layout. Below is a model of my new layout. It will drastically reduce the table size in the long run. Call me out on any misgivings I have so far, if any of you see one.

With the layout done I'm going to edit my script and run some data into it. Then try and figure out the same problem along a different path.

Any pointers is always nice. Thank you @Strawberry for the advice.

enter image description here

I am working on a DB for Sudoku and variants. My question lies with the variants. The variants are comprised of 2 or more overlapping puzzles for the 2 puzzles I figured out how.

Powershell script I'm using to build the list of Samurai x2 puzzles

$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
}

I'm stuck trying to figure out how to find 3 or more overlapping puzzles.

Samurai x3

Puz1 Region9 = Puz2 Region1

Puz2 Region9 = Puz3 Region1

I can't figure out how to differentiate Puz1 Region9 & Ruz2 Region9 and 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    |                |
+-------+---------+------+-----+---------+----------------+

Edit - In Response to @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 (both tables are identical)

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

samurai2 (samurai3 has an additional column 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 |
+----+------+-------+-------+

Query for Samurai x2 Puzzles and output, x3 would have a third 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 |
+--------+--------+

The ID used in sudoku9x9 is correlates to the IDs in region1 and region9

1

There are 1 answers

0
Ian Manseau On BEST ANSWER

After looking into normalization per @Strawberry's comment. I redid my entire DB, see my last edit to the question. With the new schema my original problem was easily solved.

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