【问题标题】:nested queries in laravel query builderlaravel 查询生成器中的嵌套查询
【发布时间】:2018-06-02 06:28:41
【问题描述】:

我需要根据请求 id 获得 1 个帖子,结构如下:

  • postId;

  • postTitle;

  • 发布内容;

  • postImage;

  • 乐队名;

  • 流派名称;

  • 标签:[tagId, tagName];

  • cmets:[commentId、commentBody、commentCreatedAt]。

表格结构:

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

  • 标签(id、名称);

  • post_tag(post_id, tag_id);

  • cmets(id、body、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]: Cardinality violation: 1241 Operand should contain 1 column(s), or other.

如何获取帖子的标签(对 cme​​ts 的查询将类似)? 无法处理这样的嵌套查询(( 我很感激任何帮助。

更新 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" 是重复的((

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    当您在 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();
    

    希望对你有帮助

    【讨论】:

    • 您必须使用 foreach 循环根据您的需要来构造结果,
    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2016-11-16
    • 2021-01-01
    • 2016-04-02
    • 2015-11-17
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多