得到这么多记录的表的左连接。php-pdo-mysql

得到这么多记录的表的左连接。php-pdo-mysql,php,mysql,sql,Php,Mysql,Sql,我留下了10个表的连接,其中4个表有3行,其余的只有一行。我想获取10个表中的所有行,因此我使用了左连接,如果其中一个表没有记录,将返回一些内容。目前我的表由1行组成,除了4个表,每个表有3行,所以我希望总共有12行。但是当我回显rowCount()时,我得到了81行它的更像是3x3x3x3 为什么会发生这种情况 如何按顺序进行正确的select查询或获得所需的12行 如有任何建议,我们将不胜感激 FYI 还尝试了union,但我认为这在我的情况下是不可能的,我只需要记录,如果它们存在,因为我知

我留下了10个表的连接,其中4个表有3行,其余的只有一行。我想获取10个表中的所有行,因此我使用了
左连接
,如果其中一个表没有记录,将返回一些内容。目前我的表由1行组成,除了4个表,每个表有3行,所以我希望总共有12行。但是当我回显
rowCount()
时,我得到了
81行
它的
更像是3x3x3x3

  • 为什么会发生这种情况
  • 如何按顺序进行正确的select查询或获得所需的12行
  • 如有任何建议,我们将不胜感激

    FYI

    还尝试了union,但我认为这在我的情况下是不可能的,我只需要记录,如果它们存在,因为我知道若有空表,union将添加null值

    添加信息

  • t1.pin作为pin存在于t1.1中
  • t1.pin存在于t2中,如表2所示(t2中有列pin,然后存在于t2.1中)
  • t2.pin作为pin存在于t2.1中
  • t2.pin作为pin存在于t2.2中
  • t1.pin在t3中以表3_pin的形式存在(t3中有列pin,该列随后存在于t3.1中)
  • t3.pin作为pin存在于t3.1中
  • t3.pin作为pin存在于t3.2中
  • t1.pin作为pin存在于t4中
  • t1.pin在t4.1中作为pin存在
  • 更新
    我想要实现的是从表中获取所有数据,这些数据将满足我放入
    的示例中的where子句,比如1
    。简言之,我希望获得表中的所有数据

    "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' ";
    
    更新的预期输出

    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  
    
    我发布的第一个预期输出肯定引起了混乱,所以我决定发布另一个。这是直接从sqlfiddle得到的,我刚刚调整了几个不正确的值。我想每年获得所有数据。我希望这一点现在已经清楚了


    我愿意接受任何其他select查询,只要我每年都能获得所有数据。要考虑的是,在某些表中,PIN可以是多个,有些表可以是空的。

    联接创建表的交叉乘积。在您的情况下,有多行具有相同的值

    pin
    ,因此联接的工作方式如下:

    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
    

    要在
    表1
    中每个条目仅显示一行,在查询结束时添加
    GROUP BY t1.sysid

    在选择之后使用DISTINCT关键字………
    选择DISTINCT t1.pin AS
    我这样做了,我删除了
    *
    我仍然得到81行如果你创建了SQLFIDLE,我可以帮助你。我在哪里可以创建SQLFIDLE?对不起,我没有这样做,你可以发布一个链接吗?先生,可能有一个链接表1中每个条目有多个条目的可能性是否有其他方法?使用唯一标识符作为外键,而不是pin?sir pin在每个表中都是唯一的,没有其他行会有pin。别针是外键,我做的。请告诉我pin(第二列)是如何唯一的:
    (1,'015-08-0011-000-01','1','2013','Active'),(2,'015-08-0011-000-01','1','2014','Active'),(3,'015-08-0011-000-01','1','2015','Active')对不起,我完全忘记了那个表。在有名称的表中,也可以有多个pin。我的问题是,当我尝试使用sysid时,它为什么不起作用(这一个是唯一的,因为它是自动递增的)我得到的不是完整的年份我只得到其他表的2013年只有表1有完整的年份
    
    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
                                                                                                  
                                                                                                  
                                                                                                  
                                                                              ...