Php 查询以获取包含或不包含group concat的多行

Php 查询以获取包含或不包含group concat的多行,php,mysql,sql,query-performance,Php,Mysql,Sql,Query Performance,我有以下表格结构: users: id PK username email salt password date_created questions: id PK user_id FK(users) title date_created date_updated answers: id PK question_id FK(questions) user_id FK(users)

我有以下表格结构:

 users:
    id PK
    username
    email
    salt
    password
    date_created

questions:
    id PK
    user_id FK(users)
    title
    date_created
    date_updated

answers:
    id PK
    question_id FK(questions)
    user_id FK(users)
    body
    date_created
因此,我需要一个查询,从这些表中获取数据,并显示用户的最后5个问题以及每个问题的前3个答案。大概是这样的:

"What is my favorite color? posted by user

red - posted by user1
green - posted by user2
yellow - posted by user3

.....
"

这就是我的目标。如何在1个查询中执行此操作?我是否应该使用group concat对一个问题的多个答案进行分组?或者我应该得到结果并在PHP中解析数组以按问题id对答案进行分组。我还需要这个查询非常快速。我不确定进行一次查询或多次查询以获取用户问题以及问题答案是否会更快。

我想我给出的关键“提示”是,您可以在组_CONCAT()中使用常规CONCAT();然后使用连接和注释中提到的一些技术来获得中间结果集,这两个函数需要获得输出。

我想我给出的关键“提示”是,您可以在组_CONCAT()中使用正则CONCAT();然后使用连接,或者使用注释中提到的一些技术,获得中间结果集,这两个函数需要这些结果集来获得输出。

作业
标记发布。勇敢,值得鼓掌!使用select-ORDERBY-limit。请参阅使用
家庭作业
标记发布的IDEASAME示例数据(首选SQLFIDLE)。勇敢,值得鼓掌!使用select-ORDERBY-limit。请参阅以获取IDEASAME示例数据(首选SQLFIDLE)