Laravel Eloquent / QueryBuilder多个嵌套选择

时间:2017-07-14 08:16:15

标签: php laravel-5 eloquent laravel-query-builder

如何在eloquent / query builder中编写类似这样的查询?,查询中有多个带连接的嵌套select语句。 任何帮助将不胜感激。

<html>
<head>
    <style>
        .active {
            color: red;
        }

    </style>
</head>
<body>
<a href="#about_me" id="about_me" class="item active" onClick="switcH('about_me')">
    About me
</a>
<a href="#experience" id="experience" class="item" onClick="switcH('experience')">
    Experiences
</a>
<a href="#education" id="education" class="item" onClick="switcH('education')">
    Education
</a>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
    function switcH(id) {

        $(".item").each(function () {
            $(this).removeClass('active');
        });
        $('#'+id).addClass('active');


    }
</script>
</body>

</html>

用户模型

    Select
          s.user_id,
          s.user_name,
          s.`user_firstname`,
          s.`user_lastname`,
          s.user_photo,
          max_rank_timestamp,
          max_friend_timestamp
    FROM
          (
            SELECT
              Users.user_id,
              Users.`user_name`,
              Users.`user_firstname`,
              Users.`user_lastname`,
              `Users`.`user_photo`,
              MAX(Ranks.rank_timestamp) AS max_rank_timestamp
            FROM
              `Users`
              LEFT JOIN Ranks ON Ranks.user_id = Users.user_id
            WHERE
              Users.user_is_active = 1
            GROUP BY
              Users.user_id
          ) s
          LEFT JOIN (
                      SELECT
                        Users.user_id,
                        MAX(Relationship.timestamp) AS max_friend_timestamp
                      FROM
                        `Users`
                        INNER JOIN Relationship ON Relationship.user_one_id = Users.user_id
                      WHERE
                        Users.user_is_active = 1
                      GROUP BY
                        Users.user_id
                    ) p ON s.user_id = p.user_id
      ) t
    ORDER BY
      `user_name` ASC

排名模型

public function ranks(){
    return $this->hasMany('App\Rank','user_id','user_id');
}

public function relationships(){
    return $this->hasMany('App\Relationship','user_one_id','user_id');
}

关系模型

public function user(){
    return $this->belongsTo('App\User','user_id','user_id');
}

1 个答案:

答案 0 :(得分:0)

您可以这样做的一种方法是将以下范围添加到ORDER BY CASE WHEN col = 1 THEN 2 ELSE 1 END, -- record with col = 1 last col -- but apart from that order by col 模型中:

User

以上假设public function scopeWithMaxRankTimestamp(Builder $query) { if (is_null($query->getQuery()->columns)) { $query->select([$query->getQuery()->from . '.*']); } $relation = Relation::noConstraints(function () { return $this->ranks(); }); $q = $this->ranks()->getRelationExistenceQuery( $relation->getRelated()->newQuery(), $query, new Expression('MAX(rank_timestamp)') ); $query->selectSub($q->toBase(), 'max_rank_timestamp'); } public function scopeWithMaxFriendTimestamp(Builder $query) { if (is_null($query->getQuery()->columns)) { $query->select([$query->getQuery()->from . '.*']); } $relation = Relation::noConstraints(function () { return $this->relationships(); }); $q = $this->relationships()->getRelationExistenceQuery( $relation->getRelated()->newQuery(), $query, new Expression('MAX(timestamp)') ); $query->selectSub($q->toBase(), 'max_friend_timestamp'); } 表的关系为Ranksranks()表的关系为Relationship

要使上述工作正常,您还需要将以下relationships()语句添加到班级的顶部:

use

然后你可以像这样使用你的结果:

use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Expression;

希望这有帮助!