dqlb38410 2012-10-26 18:29
浏览 95

从php获取sqlite pdo join查询中的数组

I would like to get a multidimensional array from my sqlite tables, with join. I have two tables:


TABLES:

- projects -----------        - images ------------------
- id                 -        - id                      -
- title              -        - filename                -
- images (has many)  -        - project_id (belongs to) -
----------------------        ---------------------------

and I want the following array


PROJECT_ARRAY
{
   id:     1
   title:  My Project
   images: IMAGES_ARRAY
   {
        image1.jpg
        image2.jpg
        image3.jpg
   }
}

how do I join my tables with sql query?

this does not work:


SELECT   project.title,        image.filename
FROM     project JOIN image
ON       image.project_id = project.id;

  • 写回答

1条回答 默认 最新

  • dongzhuo5425 2012-10-26 18:46
    关注

    You cannot get a array with a subarray from one query.

    Furthermore, your table names are plural, but your are not using the plural names in your query.

    This query should get you going:

    SELECT   p.id, p.title, i.filename
    FROM     projects p 
    JOIN     images i ON i.project_id = p.id;
    

    This should get you a result set like this:

    id: 1, title: My Project, filename: image1.jpg
    id: 1, title: My Project, filename: image2.jpg
    id: 1, title: My Project, filename: image3.jpg
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看