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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程