简体   繁体   English

Laravel 查询构建器,在日期时间进行连接和排序

[英]Laravel query builder with join and sorting on datetime

I have 3 tables: users, user_detailes, activity.我有 3 个表:users、user_detailes、activity。

Table: user表:用户

id          | username  | password  | firstname | lastname
1           | peter98   | ########  | Peter     | Parker
2           | john001   | ########  | John      | Doe
3           | kelly     | ########  | Kelly     | Johnson  

Table: user_detailes表: user_detailes

user_id     | age       | height    | hair_color
1           | 19        | 183       | Black
2           | 23        | 193       | Black
3           | 22        | 165       | Brown

Table: activity表:活动

user_id     | action    | description | timestamp
1           | SIGN_IN   | ########    | 2017-10-1 12:00:00
2           | SIGN_OUT  | ########    | 2017-10-1 10:00:00
1           | SIGN_OUT  | ########    | 2017-10-1 13:00:00
2           | SIGN_IN   | ########    | 2017-10-1 13:10:00
2           | SIGN_OUT  | ########    | 2017-10-1 13:30:00

I need a select statement with Laravel Query Builder or a SQL raw statement to get a user and his/her user_detailes and the last activity they did.我需要一个带有 Laravel Query Builder 的 select 语句或一个 SQL 原始语句来获取用户和他/她的 user_detailes 以及他们所做的最后一个活动。

For example:例如:

id          | firstname | lastname    | age     | hair_color    | last_action   | last_action_timestamp
1           | Peter     | Parker      | 19      | Black         | SIGN_OUT      | 2017-10-1 13:00:00
2           | John      | Doe         | 23      | Black         | SIGN_IN       | 2017-10-1 13:10:00
3           | Kelly     | Johnson     | 22      | Brown         | NULL          | NULL

I can't get my head around the join statement with the date sorting ;)我无法理解带有日期排序的 join 语句;)

I believe it is this as SQL query我相信这是作为 SQL 查询

SELECT u.id, u.firstname, u.lastname, ud.age, ud.hair_color, a.action, a.timestamp
FROM user u
INNER JOIN user_detailes ud ON u.id = ud.user_id
INNER JOIN activity a ON u.id = a.user_id
ORDER BY a.timestamp DESC

For a per User query I recommend using Eloquent instead对于每个用户的查询,我建议改用 Eloquent

First of all, as I can see you have one to one relationship between your user table and user_detailes table, and one to many relationships between user table and activity.首先,正如我所看到的,您的用户表和 user_detailes 表之间有一对一的关系,用户表和活动之间有一对多的关系。 that means if you join all of these tables you will have as result multi-row of one user with his user_detailes and activity because you have one to many relationships between user and activity tables.这意味着如果您加入所有这些表,您将得到一个用户的多行及其 user_detailes 和活动,因为您在用户和活动表之间存在一对多的关系。 if you want to get all data like this, the query builder bellow can help you:如果您想获得这样的所有数据,下面的查询构建器可以帮助您:

$users = DB::table('users')
        ->join('user_detailes', 'users.id', '=', 'user_detailes.user_id')
        ->join('activity', 'users.id', '=', 'activity.user_id')
        ->select('users.*', 'contacts.*', 'orders.*')
        ->orderBy('activity.timestamp', 'desc')
        ->get();

and if you want to get user_detailes and activity data of one user by his id, just you need to add where a statement like this :如果你想通过他的 id 获取一个用户的 user_detailes 和活动数据,你只需要添加一个像这样的语句:

$users = DB::table('users')
        ->join('user_detailes', 'users.id', '=', 'user_detailes.user_id')
        ->join('activity', 'users.id', '=', 'activity.user_id')
        ->select('users.*', 'contacts.*', 'orders.*')
        ->where('users.id','=',$user_id)
        ->orderBy('activity.timestamp','desc')
        ->get();

I wish this can help you我希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM