简体   繁体   English

Select 来自表 1,表 2 上的外键 = x,其中表 2 外键 = 表 3 上的 Y

[英]Select from table 1 with foreign_key = x on table 2 where table 2 foreign key = Y on table 3

Not sure if the title makes much sense, but here is a better description: I have a table of shifts that have an id for the shift worker and an id for the client the shift worker does something for, and the clients table has a user id with associated record in users table.不确定标题是否有意义,但这里有一个更好的描述:我有一个轮班表,其中有一个轮班工人的 id 和一个轮班工人为之做某事的客户的 id,并且客户表有一个用户id 与用户表中的关联记录。

I would like to select all shifts that have a client id where the associated user id on the client record is X.我想 select 所有班次都有一个客户 ID,其中客户记录上的关联用户 ID 是 X。

Shifts Table
shift_id
shiftworker_id
client_id
...

Clients Table
client_id
user_id
...

Users Table
user_id
...

So, I want all the records (from shifts) where the client_id for the shift belongs to a specific user.所以,我想要所有记录(来自班次),其中班次的 client_id 属于特定用户。

User has many clients, and client has many shifts associated with it.用户有很多客户,客户有很多与之相关的班次。 I think what I am after is some kind of join like this:我想我追求的是这样的加入:

select * from shifts join shifts.client_id on clients.id and where clients.user_id = X

I am trying to do this in Laravel, like:我正在尝试在 Laravel 中执行此操作,例如:

DB::table('shifts')->join('clients', 'client_id', '=', ...)->join('users', 'client_id', '=', X);

Although any help with the SQL is appreciated and I can figure out the Laravel query from there.尽管对 SQL 的任何帮助表示赞赏,但我可以从那里找出 Laravel 查询。

Thank you谢谢

The easiest way will be to create a "has many through relationship" and query the User model with "user id" instead of querying shifts.最简单的方法是创建“具有多个直通关系”并使用“用户 ID”查询用户 model,而不是查询班次。

class User extends Model
{
    public function shifts()
    {
        return $this->hasManyThrough(Shift::class, Client::class);
    }
}
class ShiftController extends Controller
{
    public function index()
    {
         return User::where('id', 1)->with('shifts')->first()->shifts;
    }
}

If it works, the controller code will return the records from the shifts table without any relational data or extra fields.如果有效,controller 代码将返回班次表中的记录,不带任何关系数据或额外字段。 If you need to add some extra fields, this method might not be the best one.如果您需要添加一些额外的字段,此方法可能不是最好的方法。 In this case, give more details about the desired output data.在这种情况下,请提供有关所需 output 数据的更多详细信息。

UPDATE AFTER THE COMMENT评论后更新

You don't need to use any eloquent relation function to get shift data per client, because client_id is already on the shift tables.您不需要使用任何 eloquent 关系 function 来获取每个客户的班次数据,因为 client_id 已经在班次表上。 All you need is:所有你需要的是:

Shift::where('client_id', $client_id)->get();

MySQL - 更新<values> ) 在哪里</values>集(n<table></table><div id="text_translate"><p> 只是为了让大象离开房间,因为有很多类似的问题。 我浏览了其他名称相似的帖子,但没有找到我要找的东西。 话虽如此,我可能只是没有看到如何将他们的问题实施到我的具体实施中<br><br>我正在尝试将表中的多行更新为所有 [可能] 不同的值,但我不知道在使用外键定位时我将更新多少行。 但是,由于数据库限制,可以安全地假设我有正确数量的输入。 (例如 3 行 3 个值,或 5 行 5 个值)并且即使假设不安全,我也不介意抛出错误,因为如果确实发生了错误,这将确保数据完整性......<br><br> 将哪些值分配给哪些行并不重要。<br><br> 这是我尝试过的一些示例。</p><pre class="lang-sql prettyprint-override"> UPDATE table1 SET table1.column1 IN (17, 37, 62) WHERE table1.foreign_key = 877;</pre><p> 之后我发现你不能这样使用 IN ......然后我尝试了:</p><pre class="lang-sql prettyprint-override"> UPDATE table1 SET table1.column1 = (17, 37, 62) WHERE table1.primary_key IN (SELECT primary_key FROM table1 WHERE foreign_key = 877);</pre><p> 但是,“您不能在 FROM 子句中指定目标表 'table1' 进行更新”<br><br> 对于 SELECT 然后在(n_rows + 1 个查询)之后进行更新(如下所示),这将是相当微不足道的,所以我假设可以在一个查询中完成。</p><pre class="lang-sql prettyprint-override"> SELECT primary_key from table1 where foreign_key =?;,[877]</pre><p> 然后映射值和主键(我使用的是nodejs后端)</p><pre class="lang-sql prettyprint-override"> UPDATE table1 set column1 =? where primary_key =?;,[value1,primary_key1]</pre><p> 困难的部分似乎是</p><ol><li>如何将 map 多个值添加到多行。</li><li> 如何在运行时获取每一行的唯一 ID。<br></li></ol><p> 我不介意是否必须使用存储过程,我只是真的不想向数据库发送 n+1 个查询。</p></div>.`外键`<table> </table> - MySQL - UPDATE <table> SET (n<values>) WHERE <table>.`foreign_key`

暂无
暂无

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

相关问题 创建SQL表,foreign_key混乱 - Creating SQL table, foreign_key confusion MySQL - 更新<values> ) 在哪里</values>集(n<table></table><div id="text_translate"><p> 只是为了让大象离开房间,因为有很多类似的问题。 我浏览了其他名称相似的帖子,但没有找到我要找的东西。 话虽如此,我可能只是没有看到如何将他们的问题实施到我的具体实施中<br><br>我正在尝试将表中的多行更新为所有 [可能] 不同的值,但我不知道在使用外键定位时我将更新多少行。 但是,由于数据库限制,可以安全地假设我有正确数量的输入。 (例如 3 行 3 个值,或 5 行 5 个值)并且即使假设不安全,我也不介意抛出错误,因为如果确实发生了错误,这将确保数据完整性......<br><br> 将哪些值分配给哪些行并不重要。<br><br> 这是我尝试过的一些示例。</p><pre class="lang-sql prettyprint-override"> UPDATE table1 SET table1.column1 IN (17, 37, 62) WHERE table1.foreign_key = 877;</pre><p> 之后我发现你不能这样使用 IN ......然后我尝试了:</p><pre class="lang-sql prettyprint-override"> UPDATE table1 SET table1.column1 = (17, 37, 62) WHERE table1.primary_key IN (SELECT primary_key FROM table1 WHERE foreign_key = 877);</pre><p> 但是,“您不能在 FROM 子句中指定目标表 'table1' 进行更新”<br><br> 对于 SELECT 然后在(n_rows + 1 个查询)之后进行更新(如下所示),这将是相当微不足道的,所以我假设可以在一个查询中完成。</p><pre class="lang-sql prettyprint-override"> SELECT primary_key from table1 where foreign_key =?;,[877]</pre><p> 然后映射值和主键(我使用的是nodejs后端)</p><pre class="lang-sql prettyprint-override"> UPDATE table1 set column1 =? where primary_key =?;,[value1,primary_key1]</pre><p> 困难的部分似乎是</p><ol><li>如何将 map 多个值添加到多行。</li><li> 如何在运行时获取每一行的唯一 ID。<br></li></ol><p> 我不介意是否必须使用存储过程,我只是真的不想向数据库发送 n+1 个查询。</p></div>.`外键`<table> </table> - MySQL - UPDATE <table> SET (n<values>) WHERE <table>.`foreign_key` 从具有外键列的一张表中选择? - SELECT from one table with foreign key columns? SQL Server从表中选择主键在另一个表中不显示为外键的表 - SQL Server Select from table where primary key does not appear as foreign key in another table 用外键从表中删除 - Delete from table with foreign key 根据其他表的条件选择对表的查询? 无外键 - Select query on a Table based on condition from other table ? No Foreign key 表 A 或表 B 的外键 - Foreign key to table A or table B 第一个表中的外键 - Foreign key in the first table 用外键创建表 - Creating a table with a foreign key 在外键表上排序 - Sort on foreign key table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM