使用union / join的Laravel 4复杂查询

时间:2014-10-02 01:44:49

标签: php mysql laravel-4

有人可以帮我这个吗?

当用户向管理员发送消息时,它将生成batch_id,然后当管理员向用户发送消息时,batch_id将为null。

下面是表格结构。

messages table:
---------------------------------------------------------------
id  |  sender_id | recipient_id | batch_id | subject | content
---------------------------------------------------------------
  1        3            1          $2yQr       test     test
---------------------------------------------------------------
  2        3            2          $2yQr       test     test
---------------------------------------------------------------
  3        2            3                      test2    test2
---------------------------------------------------------------

users table
------------------------
  id   |  name   | role
------------------------
   1     admin1  | ADMIN
------------------------
   2     admin2  | ADMIN
------------------------
   3      user   | USER

在上面的记录中。 具有相同batch_id的消息是用户发送给管理员的消息 然后带有空batch_id的消息是管理员发送给用户的消息。

用户应该只看到这样


id  |  sender_id | recipient_id | batch_id | subject | content
---------------------------------------------------------------
  1        3            1          $2yQr       test     test
---------------------------------------------------------------
  3        2            1                      test2    test2
---------------------------------------------------------------
到目前为止我所拥有的是

Select c.id,sender_id, recipient_id, subject, content, s.name as sender, r.name as recipient 
        from (Select id,sender_id, recipient_id, subject, content from messages where batch_id is null 
        union Select (Select b.id from messages b where b.id=a.id limit 1) as id, a.sender_id,a.recipient_id, a.subject, 
        a.content from messages a where a.batch_id is not null group by a.batch_id) as 
        c JOIN users as s ON s.id = c.sender_id JOIN users as r ON r.id = c.recipient_id where (c.sender_id =3 
        or c.recipient_id = 3)

上面的查询按预期工作,但他们希望我使用查询构建器或laravel中的雄辩

有人可以帮助我使用查询构建器或上述查询中的口才,或者有人有更好的查询,很容易转换为查询构建器或雄辩

我现在是laravel的新手

谢谢!

1 个答案:

答案 0 :(得分:3)

我想我会回答我自己的问题,以防有人遇到这个并且可能会有所帮助。

经过一段时间的挖掘后,我能够转换查询并使用laravel中的查询构建器。

        $first = \DB::table('messages as a')->select(\DB::raw('(select b.id from messages b where b.id=a.id limit 1) as id'),'a.sender_id','a.recipient_id','a.subject','a.content')
                  ->whereNotNull('a.batch_id')->groupBy('a.batch_id');
        $second = \DB::table('messages')->select('id','sender_id','recipient_id','subject','content')->whereNull('batch_id')
                  ->union($first);
        $data_query = \DB::table(\DB::raw("({$second->toSql()}) as c"))
            ->select('c.id','c.sender_id','c.recipient_id','subject','content','s.name as sender','r.name as recipient'')
            ->join('users as s','s.id','=','c.sender_id')
            ->join('users as r','r.id','=','c.recipient_id')
            ->whereRaw('(sender_id =' .$id . ' OR ' . 'recipient_id ='. $id.')')
            ->whereRaw('(subject LIKE '. '"%' .$subject.'%"' .' OR ' . 'content LIKE ' . '"%' .$subject.'%")' )
            ->whereRaw('(s.email LIKE '. '"%' .$sender.'%"' .' OR ' . 's.name LIKE ' . '"%' .$sender.'%")' )
            ->get();