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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)