php - Laravel 查询生成器中的嵌套查询

标签 php mysql laravel

我需要根据请求 ID 获取 1 个帖子,其结构如下:

  • 帖子ID;

  • 帖子标题;

  • 帖子内容;

  • postImage;

  • 乐队名称;

  • 流派名称;

  • 标签:[tagId,tagName];

  • 评论:[commentId、commentBody、commentCreatedAt]。

表结构:

  • 帖子(id、标题、内容、图片、band_id、时间戳);

  • 标签(id、名称);

  • post_tag(post_id, tag_id);

  • 评论(id、正文、post_id、user_id、时间戳)。

我尝试了不同的查询变体,例如:

$post = DB::table('posts as p')
    ->select('p.id as postId',
        'p.title as postTitle',
        'p.content as postContent',
        'p.image as postImage',
        'b.name as bandName',
        'g.name as genreName',
            DB::raw("(SELECT t.id as tagId, t.name as tagName
                     FROM tags as t
                     JOIN post_tag as pt ON t.id = pt.tag_id
                     WHERE pt.post_id = $request->postId
                     GROUP BY tagId) as tags"))
    ->join('bands as b', 'b.id', 'p.band_id')
    ->join('genres as g', 'g.id', 'b.genre_id')
    ->where('p.id', $request->postId)
    ->groupBy(
        'postId',
        'postTitle',
        'postContent',
        'postImage',
        'bandName',
        'genreName')
    ->get();

但是我在获取标签时遇到了困难((它返回错误:SQLSTATE[21000]:基数违规:1241 操作数应包含 1 列或其他列。

如何获取帖子的标签(评论查询类似)? 无法处理此类嵌套查询(( 我很感激任何帮助。

更新 1。

尝试过:

$post = DB::table('posts as p')
        ->select('p.id as postId',
            'p.title as postTitle',
            'p.content as postContent',
            'p.image as postImage',
            'b.name as bandName',
            'g.name as genreName',
            't.id as tagId',
            't.name as tagName')
        ->join('post_tag as pt', 'p.id', 'pt.post_id')
        ->join('tags as t', 't.id', 'pt.tag_id')
        ->join('bands as b', 'b.id', 'p.band_id')
        ->join('genres as g', 'g.id', 'b.genre_id')
        ->where('p.id', $request->postId)
        ->groupBy(
            'postId',
            'postTitle',
            'postContent',
            'postImage',
            'bandName',
            'genreName',
            'tagId')
        ->get();

结果:

[{
    "postId",
    "postTitle",
    "postContent",
    "postImage",
    "bandName",
    "genreName",
    "tagId",
    "tagName"
},{
    "postId",
    "postTitle",
    "postContent",
    "postImage",
    "bandName",
    "genreName",
    "tagId",
    "tagName"
}]

因此,“postId”、“postTitle”、“postContent”、“postImage”、“bandName”、“genreName”是重复的((

最佳答案

当您在 select 语句中使用子查询时,它必须返回一个列值,这就是您收到此错误的原因,但是如果您也想获取标签名称和 id,为什么不只添加另一个与标签和 post_tag 表的连接,这里是一个例子:

$post = DB::table('posts as p')
->select('p.id as postId',
    'p.title as postTitle',
    'p.content as postContent',
    'p.image as postImage',
    'b.name as bandName',
    'g.name as genreName',
    't.id as tagId',
    't.name as tagName')
->join('post_tag as pt', 'p.id', 'pt.post_id')
->join('tags as t', 't.tag_id', 'pt.tag_id')
->join('bands as b', 'b.id', 'p.band_id')
->join('genres as g', 'g.id', 'b.genre_id')
->where('p.id', $request->postId)
->groupBy(
    'postId',
    'postTitle',
    'postContent',
    'postImage',
    'bandName',
    'genreName')
->get();

希望这有帮助

关于php - Laravel 查询生成器中的嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47893434/

相关文章:

php - 将多维数组分解为 PHP 中的单个逗号分隔列表

mysql - 使用大小写按计算列进行过滤和排序(未找到列)

javascript - SQL 函数,其中我将 "where statement"用于另一个 "where statement"中的一组数据

php - 使用FOR循环消除数组检索到的数字

javascript - 如何使用ajax发送文件和文本

MySQL 第二个抽象连接显着减慢了查询速度

laravel - 如何在 Eloquent 上静态地通过关系使用 where 子句?

laravel - 在 Laravel 上访问 redis 流水线功能之外的变量

php - 从已输入关键字的下一行获取数据

php - PHP和多重继承;我知道你做不到,但是我怎么..?