doupa2871 2014-12-09 13:27 采纳率: 100%
浏览 23
已采纳

Php / SQLite,使用从查询中获取的JOIN参数?

I have to do this with PHP on a simple SQLite database , with PDO

i have 3 table, tableA - tableB, TableC

tableA

the column TYPE represents the name of the other tables

___________________________________
|ID_OBJ |  TYPE  | PROP_1 | PROP_2 |
-----------------------------------
| 1000  | tableB |    0   |  10    |
| 1001  | tableB |    1   |  10    |
| 1002  | tableC |    1   |  10    |
-----------------------------------

tableB

____________________________
|   ID  |  PROPA   | PROPB  |
----------------------------
| 1000  |  ...     |  ....  |
| 1001  |  ...     |  ....  |
----------------------------

tableC

____________________________
|   ID  | PROPAA   | PROPBB |
----------------------------
| 1002  |  ...     |  ....  |
----------------------------

what I wanted to know is: Can i , through a JOIN (for example a LEFT JOIN) do a query that return something like this?

_______________________________________________________________________
|ID_OBJ |  TYPE  | PROP_1 | PROP_2 | PROPA   | PROPB | PROPAA   | PROPBB|
-----------------------------------------------------------------------
| 1000  | tableB |    0   |  10    |   ...   |   ... |          |       |
| 1001  | tableB |    1   |  10    |   ...   |   ... |          |       |
| 1002  | tableC |    1   |  10    |         |       |  ...     |  ...  |
-------------------------------------------------------------------------

I don't know how to do this, having to take the name of the table on which to perform a join from a table. So let me know if I can run a JOIN, and not having to do a query for each row present in the table tableA

thanks

  • 写回答

1条回答 默认 最新

  • douxing7101 2014-12-09 13:53
    关注

    An outer join also returns rows for which no match was found:

    SELECT tableA.ID_OBJ,
           tableA.TYPE,
           tableA.PROP_1,
           tableA.PROP_2,
           tableB.PROPA,
           tableB.PROPB,
           tableC.PROPAA,
           tableC.PROPBB
    FROM tableA
    LEFT JOIN tableB ON tableA.TYPE = 'tableB' AND tableA.ID_OBJ = tableB.ID
    LEFT JOIN tableC ON tableA.TYPE = 'tableC' AND tableA.ID_OBJ = tableC.ID
    

    If the are no 'wrong' rows in tableB/C, it is not necessary to check the type:

    SELECT tableA.ID_OBJ,
           tableA.TYPE,
           tableA.PROP_1,
           tableA.PROP_2,
           tableB.PROPA,
           tableB.PROPB,
           tableC.PROPAA,
           tableC.PROPBB
    FROM tableA
    LEFT JOIN tableB ON tableA.ID_OBJ = tableB.ID
    LEFT JOIN tableC ON tableA.ID_OBJ = tableC.ID
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?