0

I am working on a view that joins multiple table. The data will be entered on separate table based on RepairID. The view will gather the data that is linking to the RepairID. My problem is after I enter the data, the view will show all the probability of the records output. Below are my code of view:

SELECT 
    `new`.`SRPartsID` AS `SRPartsID`,
    `new`.`RepairID` AS `RepairID`,
    `new`.`SRNo` AS `SRNo`,
    `new`.`DateReceived` AS `DateReceived`,
    `new`.`ShipmentDate` AS `ShipmentDate`,
    `tb1stdebug`.`FirstDebugTestingErrorCode` AS `FirstDebugTestingErrorCode`,
    `tb1stdebug`.`FirstDebugActionTaken` AS `FirstDebugActionTaken`,
    `tb1stdebug`.`FirstDebugComponentLocation` AS `FirstDebugComponentLocation`,
    `tb2nddebug`.`SecondDebugTestingErrorCode` AS `SecondDebugTestingErrorCode`,
    `tb2nddebug`.`SecondDebugActionTaken` AS `SecondDebugActionTaken`,
    `tb2nddebug`.`SecondDebugComponentLocation` AS `SecondDebugComponentLocation`,
    `tb3rddebug`.`ThirdDebugTestingErrorCode` AS `ThirdDebugTestingErrorCode`,
    `tb3rddebug`.`ThirdDebugActionTaken` AS `ThirdDebugActionTaken`,
    `tb3rddebug`.`ThirdDebugComponentLocation` AS `ThirdDebugComponentLocation`,
    `tb1stfct`.`FirstFctTestingErrorCode` AS `FirstFctTestingErrorCode`,
    `tb1stfct`.`FirstFctActionTaken` AS `FirstFctActionTaken`,
    `tb1stfct`.`FirstFctComponentLocation` AS `FirstFctComponentLocation`,
    `tb2ndfct`.`SecondFctTestingErrorCode` AS `SecondFctTestingErrorCode`,
    `tb2ndfct`.`SecondFctActionTaken` AS `SecondFctActionTaken`,
    `tb2ndfct`.`SecondFctComponentLocation` AS `SecondFctComponentLocation`,
    `tb3rdfct`.`ThirdFctTestingErrorCode` AS `ThirdFctTestingErrorCode`,
    `tb3rdfct`.`ThirdFctActionTaken` AS `ThirdFctActionTaken`,
    `tb3rdfct`.`ThirdFctComponentLocation` AS `ThirdFctComponentLocation`
FROM
    ((((((`tbsrparts_new` `new`
    LEFT JOIN `tb1stdebug` ON ((`new`.`RepairID` = `tb1stdebug`.`RepairID`)))
    LEFT JOIN `tb2nddebug` ON ((`new`.`RepairID` = `tb2nddebug`.`RepairID`)))
    LEFT JOIN `tb3rddebug` ON ((`new`.`RepairID` = `tb3rddebug`.`RepairID`)))
    LEFT JOIN `tb1stfct` ON ((`new`.`RepairID` = `tb1stfct`.`RepairID`)))
    LEFT JOIN `tb2ndfct` ON ((`new`.`RepairID` = `tb2ndfct`.`RepairID`)))
    LEFT JOIN `tb3rdfct` ON ((`new`.`RepairID` = `tb3rdfct`.`RepairID`)))
ORDER BY `new`.`SRPartsID` DESC

This is my table structure

tbsrparts_new

+-------------------------------+--------------------+------+-----+---------+----------------+
|             Field             |        Type        | Null | Key | Default |     Extra      |
+-------------------------------+--------------------+------+-----+---------+----------------+
| SRPartsID                     | "int(10) unsigned" | NO   | PRI | NULL    | auto_increment |
| RepairID                      | varchar(200)       | NO   |     | NULL    |                |
| SRNo                          | varchar(200)       | YES  | MUL | NULL    |                |
| DateReceived                  | varchar(200)       | YES  |     | NULL    |                |
| ShipmentDate                  | varchar(200)       | YES  |     | NULL    
+-------------------------------+--------------------+------+-----+---------+----------------+

tb1stdebug

+-----------------------------+--------------------+------+-----+---------+----------------+
|            Field            |        Type        | Null | Key | Default |     Extra      |
+-----------------------------+--------------------+------+-----+---------+----------------+
| FirstDebugID                | "int(10) unsigned" | NO   | PRI | NULL    | auto_increment |
| RepairID                    | varchar(500)       | YES  |     | NULL    |                |
| FirstDebugTestingErrorCode  | varchar(500)       | YES  | MUL | NULL    |                |
| FirstDebugActionTaken       | varchar(500)       | YES  |     | NULL    |                |
| FirstDebugComponentLocation | varchar(500)       | YES  |     | NULL    |                |
+-----------------------------+--------------------+------+-----+---------+----------------+

tb2nddebug

+-----------------------------+--------------------+------+-----+---------+----------------+
|            Field            |        Type        | Null | Key | Default |     Extra      |
+-----------------------------+--------------------+------+-----+---------+----------------+
| SecondDebugID                | "int(10) unsigned" | NO   | PRI | NULL    | auto_increment |
| RepairID                     | varchar(500)       | YES  |     | NULL    |                |
| SecondDebugTestingErrorCode  | varchar(500)       | YES  | MUL | NULL    |                |
| SecondDebugActionTaken       | varchar(500)       | YES  |     | NULL    |                |
| SecondDebugComponentLocation | varchar(500)       | YES  |     | NULL    |                |
+-----------------------------+--------------------+------+-----+---------+----------------+

tb3rddebug

+-----------------------------+--------------------+------+-----+---------+----------------+
|            Field            |        Type        | Null | Key | Default |     Extra      |
+-----------------------------+--------------------+------+-----+---------+----------------+
| ThirdDebugID                | "int(10) unsigned" | NO   | PRI | NULL    | auto_increment |
| RepairID                    | varchar(500)       | YES  |     | NULL    |                |
| ThirdDebugTestingErrorCode  | varchar(500)       | YES  | MUL | NULL    |                |
| ThirdDebugActionTaken       | varchar(500)       | YES  |     | NULL    |                |
| ThirdDebugComponentLocation | varchar(500)       | YES  |     | NULL    |                |
+-----------------------------+--------------------+------+-----+---------+----------------+

tb1stfct

+---------------------------+--------------------+------+-----+---------+----------------+
|           Field           |        Type        | Null | Key | Default |     Extra      |
+---------------------------+--------------------+------+-----+---------+----------------+
| FirstFctID                | "int(10) unsigned" | NO   | PRI | NULL    | auto_increment |
| RepairID                  | varchar(45)        | YES  |     | NULL    |                |
| FirstFctTestingErrorCode  | varchar(500)       | YES  | MUL | NULL    |                |
| FirstFctActionTaken       | varchar(500)       | YES  |     | NULL    |                |
| FirstFctComponentLocation | varchar(500)       | YES  |     | NULL    |                |
+---------------------------+--------------------+------+-----+---------+----------------+

tb2ndfct

+----------------------------+--------------------+------+-----+---------+----------------+
|           Field            |        Type        | Null | Key | Default |     Extra      |
+----------------------------+--------------------+------+-----+---------+----------------+
| SecondFctID                | "int(10) unsigned" | NO   | PRI | NULL    | auto_increment |
| RepairID                   | varchar(45)        | YES  |     | NULL    |                |
| SecondFctTestingErrorCode  | varchar(500)       | YES  | MUL | NULL    |                |
| SecondFctActionTaken       | varchar(500)       | YES  |     | NULL    |                |
| SecondFctComponentLocation | varchar(500)       | YES  |     | NULL    |                |
+----------------------------+--------------------+------+-----+---------+----------------+

tb3rdfct

+---------------------------+--------------------+------+-----+---------+----------------+
|           Field           |        Type        | Null | Key | Default |     Extra      |
+---------------------------+--------------------+------+-----+---------+----------------+
| ThirdFctID                | "int(10) unsigned" | NO   | PRI | NULL    | auto_increment |
| RepairID                  | varchar(45)        | YES  |     | NULL    |                |
| ThirdFctTestingErrorCode  | varchar(500)       | YES  | MUL | NULL    |                |
| ThirdFctActionTaken       | varchar(500)       | YES  |     | NULL    |                |
| ThirdFctComponentLocation | varchar(500)       | YES  |     | NULL    |                |
+---------------------------+--------------------+------+-----+---------+----------------+

This is my sample data inserted to the db

tbsrparts_new

+-----------+----------+-------+--------------+--------------+
| SRPartsID | RepairID | SRNo  | DateReceived | ShipmentDate |
+-----------+----------+-------+--------------+--------------+
|     26050 |    26041 | test2 | 02,July,2015 | Pending      |
+-----------+----------+-------+--------------+--------------+

tb1stdebug

+--------------+----------+----------------------------+-----------------------+-----------------------------+
| FirstDebugID | RepairID | FirstDebugTestingErrorCode | FirstDebugActionTaken | FirstDebugComponentLocation |
+--------------+----------+----------------------------+-----------------------+-----------------------------+
|        26048 |    26041 | T00111                     | Touch Up              | -                           |
|        26049 |    26041 | T02222                     | Retest                | -                           |
|        26053 |    26041 | T08888                     | Touch Up              | -                           |
+--------------+----------+----------------------------+-----------------------+-----------------------------+

tb2nddebug

+---------------+----------+-----------------------------+------------------------+------------------------------+
| SecondDebugID | RepairID | SecondDebugTestingErrorCode | SecondDebugActionTaken | SecondDebugComponentLocation |
+---------------+----------+-----------------------------+------------------------+------------------------------+
|            12 |    26041 | T03333                      | Touch Up               | -                            |
|            13 |    26041 | T04444                      | Retest                 | -                            |
|            14 |    26041 | T08888                      | Touch Up               | -                            |
+---------------+----------+-----------------------------+------------------------+------------------------------+

tb3rddebug

+--------------+----------+----------------------------+-----------------------+-----------------------------+
| ThirdDebugID | RepairID | ThirdDebugTestingErrorCode | ThirdDebugActionTaken | ThirdDebugComponentLocation |
+--------------+----------+----------------------------+-----------------------+-----------------------------+
|           12 |    26041 | T03333                     | Touch Up              | -                           |
|           13 |    26041 | T04444                     | Retest                | -                           |
|           14 |    26041 | T08888                     | Touch Up              | -                           |
+--------------+----------+----------------------------+-----------------------+-----------------------------+

tb1stfct

+------------+----------+--------------------------+---------------------+---------------------------+
| FirstFctID | RepairID | FirstFctTestingErrorCode | FirstFctActionTaken | FirstFctComponentLocation |
+------------+----------+--------------------------+---------------------+---------------------------+
|      26052 |    26041 | T08888                   | Retest              | -                         |
+------------+----------+--------------------------+---------------------+---------------------------+

tb2ndfct

+-------------+----------+---------------------------+----------------------+----------------------------+
| SecondFctID | RepairID | SecondFctTestingErrorCode | SecondFctActionTaken | SecondFctComponentLocation |
+-------------+----------+---------------------------+----------------------+----------------------------+
|           9 |    26041 | T08888                    | Touch Up             | -                          |
|          10 |    26041 | T88889                    | Retest               | -                          |
+-------------+----------+---------------------------+----------------------+----------------------------+

tb3rdfct

+------------+----------+--------------------------+---------------------+---------------------------+
| ThirdFctID | RepairID | ThirdFctTestingErrorCode | ThirdFctActionTaken | ThirdFctComponentLocation |
+------------+----------+--------------------------+---------------------+---------------------------+
|          7 |    26041 | T08888                   | Touch Up            | -                         |
+------------+----------+--------------------------+---------------------+---------------------------+

The output on the view shows all the probability of the combination since some of the table has more than 1 records that referring to the same RepairID.Below are the output in image format. I cant paste the table because it reached the maximum characters.

Output

This is the output i want

+-------+---------------+--------------+----------------------------+-----------------------+-----------------------------+-----------------------------+------------------------+------------------------------+----------------------------+-----------------------+-----------------------------+--------------------------+---------------------+---------------------------+---------------------------+----------------------+----------------------------+--------------------------+---------------------+---------------------------+
| SRNo  | DateReceived  | ShipmentDate | FirstDebugTestingErrorCode | FirstDebugActionTaken | FirstDebugComponentLocation | SecondDebugTestingErrorCode | SecondDebugActionTaken | SecondDebugComponentLocation | ThirdDebugTestingErrorCode | ThirdDebugActionTaken | ThirdDebugComponentLocation | FirstFctTestingErrorCode | FirstFctActionTaken | FirstFctComponentLocation | SecondFctTestingErrorCode | SecondFctActionTaken | SecondFctComponentLocation | ThirdFctTestingErrorCode | ThirdFctActionTaken | ThirdFctComponentLocation |
+-------+---------------+--------------+----------------------------+-----------------------+-----------------------------+-----------------------------+------------------------+------------------------------+----------------------------+-----------------------+-----------------------------+--------------------------+---------------------+---------------------------+---------------------------+----------------------+----------------------------+--------------------------+---------------------+---------------------------+
| test2 | 2, July, 2015 | Pending      | T00111                     | Touch Up              | -                           | T03333                      | Touch Up               | -                            | T05555                     | Touch Up              | -                           | T08888                   | Retest              | -                         | T08888                    | Touch Up             | -                          | T08888                   | Touch Up            | -                         |
| test2 | 2, July, 2015 | Pending      | T02222                     | Retest                | -                           | T04444                      | Retest                 | -                            | T06666                     | Retest                | -                           | T08888                   | Retest              | -                         | T08889                    | Retest               | -                          | T08888                   | Touch Up            | -                         |
| test2 | 2, July, 2015 | Pending      | T08888                     | Touch Up              | -                           | T08888                      | Touch Up               | -                            | T08888                     | Touch Up              | -                           | T08888                   | Retest              | -                         | T08889                    | Retest               | -                          | T08888                   | Touch Up            | -                         |
+-------+---------------+--------------+----------------------------+-----------------------+-----------------------------+-----------------------------+------------------------+------------------------------+----------------------------+-----------------------+-----------------------------+--------------------------+---------------------+---------------------------+---------------------------+----------------------+----------------------------+--------------------------+---------------------+---------------------------+
7
  • 1
    This would be the expected behavior since you haven't specified how the joins should work other than 'RepairID'. For instance, if you had just the 3 tables 'new', 'tb1stdebug', and 'tb2nddebug' - you haven't clarified what the correlation is between 'tb1stdebug' and 'tb2nddebug'. As such, you'll get a cartesian product of these 2 tables until you do.
    – PinnyM
    Jul 6, 2015 at 4:03
  • @PinnyM, The tables has no relationship. The only thing common in these table is RepairID. Jul 6, 2015 at 7:14
  • Exactly, because there is no specific relationship, the JOINs will return every combination of rows where the RepairID matches.
    – PinnyM
    Jul 6, 2015 at 14:17
  • Hi, I am not sure on how to define the specific relationship. I have also created the sqlfiddle : sqlfiddle.com/#!9/d155d/1. Appreciate if you can help. Jul 7, 2015 at 5:36
  • Perhaps you should show your desired output to make things clearer Jul 7, 2015 at 9:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.