表的左连接获得了如此多的记录。 php pdo mysql

时间:2015-10-07 04:10:52

标签: php mysql sql

"SELECT *,t1.pin AS table1.1pin"
        . ",t3.pin AS table2.1pin"
        . ",t6.pin AS table3.1pin"
        . ",t9.pin AS table4.1pin"
        . ",t2.tin AS table1.1tin"
        . ",t2.first_name AS table1.1firstname"
        . ",t2.last_name AS table1.1lastname"
        . ",t2.middle_name AS table1.1middlename"
        . ",t2.suffix AS table1.1suffix"
        . ",t5.tin AS table2.1tin"
        . ",t5.first_name AS table2.1firstname"
        . ",t5.last_name AS table2.1lastname"
        . ",t5.middle_name AS table2.1middlename"
        . ",t5.suffix AS table2.1suffix"
        . ",t8.tin AS table3.1tin"
        . ",t8.first_name AS table3.1firstname"
        . ",t8.last_name AS table3.1lastname"
        . ",t8.middle_name AS table3.1middlename"
        . ",t8.suffix AS table3.1suffix"
        . ",t10.tin AS table4.1tin"
        . ",t10.first_name AS table4.1firstname"
        . ",t10.last_name AS table4.1lastname"
        . ",t10.middle_name AS table4.1middlename"
        . ",t10.suffix AS table4.1suffix"
        . ",t1.effectivity_qtr AS table1qtr"
        . ",t1.effectivity_year AS table1year"
        . ",t4.effectivity_qtr AS table2qtr"
        . ",t4.effectivity_year AS table2year"
        . ",t7.effectivity_qtr AS table3qtr"
        . ",t7.effectivity_year AS table3year"
        . ",t9.effectivity_qtr AS table4qtr"
        . ",t9.effectivity_year AS table4year"
        . " FROM "
        . "table1 AS t1 "
        . "LEFT JOIN table1.1 AS t2 ON t1.pin = t2.pin AND t1.status = t2.status "
        . "LEFT JOIN table2 AS t3 ON t1.pin= t3.table2_pin AND t1.status = t3.status "
        . "LEFT JOIN table2.1 AS t4 ON t3.pin = t4.pin AND t3.status = t4.status "
        . "LEFT JOIN table2.2 AS t5 ON t3.pin = t5.pin AND t3.status = t5.status "
        . "LEFT JOIN table3 AS t6 ON t1.pin = t6.table3_pin AND t1.status = t6.status "
        . "LEFT JOIN table3.1 AS t7 ON t6.pin = t7.pin AND t6.status = t7.status "
        . "LEFT JOIN table3.2 AS t8 ON t6.pin = t8.pin AND t6.status = t8.status "
        . "LEFT JOIN table4 AS t9 ON t1.pin = t9.pin AND t1.status = t9.status "
        . "LEFT JOIN table4.1 AS t10 ON t1.pin = t10.pin AND t1.status = t10.status "
        . "WHERE "
        . "t1.pin LIKE '%$pin%' AND t1.status = 'Active' ";

我已经离开了10个表的连接,其中4个表各有3行,其余只有一行。我想获取10个表中的所有行,所以我使用LEFT JOIN,如果其中一个表没有记录,那么将会返回一些内容。目前我的表每行包含1行,除了4个表,每个表有3行,所以我希望总共得到12行。但当我回复rowCount()时,我81 rows more like 3 X 3 X 3 X 3

  1. 为什么会这样。
  2. 如何按顺序进行正确的选择查询或获取所需的12行。
  3. 任何建议表示赞赏。

    FYI

    也试过联盟,但我认为在我的情况下我不需要记录,如果它们存在,因为我知道如果有空表,union将添加空值。

    ADDITION INFO

    1. t1.pin作为pin
    2. 存在于t1.1中
    3. t1.pin作为table2_pin存在于t2中(t2具有列引脚,然后存在于t2.1中)
    4. t2.pin作为pin
    5. 存在于t2.1中
    6. t2.pin作为pin
    7. 存在于t2.2中
    8. t1.pin在t3中表示为table3_pin(t3具有列引脚,然后存在于t3.1中)
    9. t3.pin在t3.1中作为pin
    10. 存在
    11. t3.pin作为pin
    12. 存在于t3.2中
    13. t1.pin在t4中作为pin
    14. 存在
    15. t1.pin作为pin
    16. 存在于t4.1中

      DEMO HERE

      UPDATED DEMO HERE

      UPDATE 我想要实现的是获取表格中的所有数据,这些数据将满足我放入like 1的此示例中的where子句。简而言之,我希望获得表格中的所有数据。

      pin                          effectivity_qtr    effectivity_year    status      tin             last_name   first_name  middle_name suffix      
      015-08-0011-000-01           1st                2013                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01           1st                2014                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01           1st                2015                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01-1001      1st                2013                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01-1001      1st                2014                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01-1001      1st                2015                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01-2001      1st                2013                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01-2001      1st                2014                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01-2001      1st                2015                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01           1st                2013                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01           1st                2014                Active      342-432-423-000 Smith       John        James       Jr  
      015-08-0011-000-01           1st                2015                Active      342-432-423-000 Smith       John        James       Jr  
      

      UPDATED EXPECTED OUTPUT

      sysid   pin effectivity_qtr effectivity_year    status  sys_id  tin pin last_name   first_name  middle_name suffix  status  sys_id  pin table2_pin  status  sysid   pin effectivity_qtr effectivity_year    status  sys_id  tin pin last_name   first_name  middle_name suffix  status  sys_id  pin table3_pin  status  sys_id  pin status  effectivity_qtr effectivity_year    sys_id  tin pin last_name   first_name  middle_name suffix  status  sys_id  pin status  effectivity_qtr effectivity_year    sys_id  tin pin last_name   first_name  suffix  middle_name status  pin pin pin pin tin first_name  last_name   middle_name suffix  tin first_name  last_name   middle_name suffix  tin first_name  last_name   middle_name suffix  tin first_name  last_name   middle_name suffix  effectivity_qtr effectivity_year    effectivity_qtr effectivity_year    effectivity_qtr effectivity_year    effectivity_qtr effectivity_year
      1   015-08-0011-000-01  1st 2013    Active  1   342-432-423-000 015-08-0011-000-01  Smith   John    James   Jr  Active  1   015-08-0011-000-01-1001 015-08-0011-000-01  Active  1   015-08-0011-000-01-1001 1st 2013    Active  1   342-432-423-000 015-08-0011-000-01-1001 Smith   John    James   Jr  Active  1   015-08-0011-000-01-2001 015-08-0011-000-01  Active  1   015-08-0011-000-01-2001 Active  1st 2013    1   342-432-423-000 015-08-0011-000-01-2001 Smith   John    James   Jr  Active  1   015-08-0011-000-01  Active  1st 2013    1   342-432-423-000 015-08-0011-000-01  Smith   John    Jr  James   Active  015-08-0011-000-01  015-08-0011-000-01-1001 015-08-0011-000-01-2001 015-08-0011-000-01  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  1st 2013    1st 2013    1st 2013    1st 2013
      2   015-08-0011-000-01  1st 2014    Active  1   342-432-423-000 015-08-0011-000-01  Smith   John    James   Jr  Active  1   015-08-0011-000-01-1001 015-08-0011-000-01  Active  1   015-08-0011-000-01-1001 1st 2014    Active  1   342-432-423-000 015-08-0011-000-01-1001 Smith   John    James   Jr  Active  1   015-08-0011-000-01-2001 015-08-0011-000-01  Active  1   015-08-0011-000-01-2001 Active  1st 2014    1   342-432-423-000 015-08-0011-000-01-2001 Smith   John    James   Jr  Active  1   015-08-0011-000-01  Active  1st 2014    1   342-432-423-000 015-08-0011-000-01  Smith   John    Jr  James   Active  015-08-0011-000-01  015-08-0011-000-01-1001 015-08-0011-000-01-2001 015-08-0011-000-01  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  1st 2014    1st 2014    1st 2014    1st 2014
      3   015-08-0011-000-01  1st 2015    Active  1   342-432-423-000 015-08-0011-000-01  Smith   John    James   Jr  Active  1   015-08-0011-000-01-1001 015-08-0011-000-01  Active  1   015-08-0011-000-01-1001 1st 2015    Active  1   342-432-423-000 015-08-0011-000-01-1001 Smith   John    James   Jr  Active  1   015-08-0011-000-01-2001 015-08-0011-000-01  Active  1   015-08-0011-000-01-2001 Active  1st 2015    1   342-432-423-000 015-08-0011-000-01-2001 Smith   John    James   Jr  Active  1   015-08-0011-000-01  Active  1st 2015    1   342-432-423-000 015-08-0011-000-01  Smith   John    Jr  James   Active  015-08-0011-000-01  015-08-0011-000-01-1001 015-08-0011-000-01-2001 015-08-0011-000-01  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  342-432-423-000 John    Smith   James   Jr  1st 2015    1st 2015    1st 2015    1st 2015
       Record Count: 3; Execution Time: 8ms View Execution Plan  link
      Did this query solve the problem? If so, consider donating $5 to help make sure SQL Fiddle will be here next time you need help with a database problem. Thanks!
      SQL Sentry  CollectServer.info - Easy Server Health Monitoring
      

      我发布的第一个预期输出一定是混乱所以我决定发布另一个。这是直接从sqlfiddle我只是调整了一些不正确的值。我想每年获得所有数据。我希望现在已经清楚了。

      只要我每年都可以获得所有数据,我就可以使用任何其他选择查询。要考虑的事情是pin在某些表中可能是多个,有些表可能是空的。

1 个答案:

答案 0 :(得分:0)

联接创建表格的交叉产品。在您的情况下,有多个行具有相同的pin值,因此连接的工作方式如下:

table1                          table2_                             table3__                  
                                                                                              
                                1. 015-08-0011-000-01-1001 +------> 1. 015-08-0011-000-01-1001
1. 015-08-0011-000-01-1001 +--> 2. 015|08|0011|000|01|1001+-----+   2. 015|08|0011|000|01|1001
                                3. 015-08-0011-000-01-1001 +-+  |   3. 015-08-0011-000-01-1001
                                                             |  |                             
                                1. 015-08-0011-000-01-1001   |  |   1. 015-08-0011-000-01-1001
2. 015-08-0011-000-01-1001 +--> 2. 015|08|0011|000|01|1001   |  +-> 2. 015|08|0011|000|01|1001
                                3. 015-08-0011-000-01-1001   |      3. 015-08-0011-000-01-1001
                                                             |                                
                                1. 015-08-0011-000-01-1001   |      1. 015-08-0011-000-01-1001
3. 015-08-0011-000-01-1001 +--> 2. 015|08|0011|000|01|1001   +----> 2. 015|08|0011|000|01|1001
                                3. 015-08-0011-000-01-1001          3. 015-08-0011-000-01-1001
                                                                                              
                                                                                              
                                                                                              
                                                                          ...                 

要在table1中为每个条目准确显示一行,请在查询末尾添加GROUP BY t1.sysid