laravel查询构建器中的嵌套查询

时间:2017-12-19 18:52:07

标签: php mysql laravel

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

  • 帖子ID;

  • postTitle;

  • postContent;

  • postImage;

  • bandName;

  • genreName;

  • 标签:[tagId,tagName];

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

表格结构:

  • 帖子(id,title,content,image,band_id,timestamps);

  • 标签(id,name);

  • post_tag(post_id,tag_id);

  • 评论(id,body,post_id,user_id,timestamps)。

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

  import React from 'react';
  import IconButton from 'material-ui/IconButton';
   export class IconButtonExampleSimple extends React.Component{

   render ()
  {
   return (<div>
             <IconButton iconClassName="muidocs-icon-custom-github" />
             <IconButton iconClassName="muidocs-icon-custom-github" 
             disabled={true} />
           </div>
          );
   }
   }

但我卡住了标签((它返回错误: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',
            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();

结果:

$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();

所以,的&#34;帖子ID&#34;&#34; postTitle&#34;&#34; postContent&#34;&#34; postImage&#34;&#34; bandName&# 34;,&#34; genreName&#34;重复(

1 个答案:

答案 0 :(得分:0)

当你在select语句中使用子查询时,它必须返回一个列值,这就是你得到这个错误的原因,但是如果你想获得标签name和id,为什么不只是添加另一个带有tags和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();

希望这有帮助