dongyuli4538 2015-01-03 22:44
浏览 26
已采纳

替换连接3个表而不重复

I have 3 tables in mysql, table A, B and C.
C has a relationship with B (b_id) and B has a relationship with A (a_id).

that means A has many B, and B has many C. To select all, I have this query:

SELECT a.id, a.name, a.text, b.ptext, c.ctext
FROM tableA as a
JOIN 
  tableB as b 
ON 
  (b.p_id = a.id)
JOIN 
  tableC as c 
ON 
  (c.p_pid = b.pid)
WHERE a.id = 1

which returns this:

| ID |  NAME |         TEXT |  BTEXT |  CTEXT |
|----|-------|--------------|--------|--------|
|  1 | page1 | futkdvthsa_1 | post_1 | thing1 |
|  1 | page1 | futkdvthsa_1 | post_2 | thing2 |
|  1 | page1 | futkdvthsa_1 | post_2 | thing3 |

is there any chance of getting something like this:

id |  name |         text | posts
 1 | page1 | futkdvthsa_1 | posts (post_1 = ( thing1 ), post_2 = ( thing2, thing3 ) )

Sigle queries for each table or would need to do in a php way?

P.S. this would be for a wordpress plugin with custom tables.

  • 写回答

1条回答 默认 最新

  • douchunji1885 2015-01-03 22:50
    关注

    I guess the simplest way to do this is by doing it with php. You would have to use a stored procedure to concatenate your fields with sql.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效