简体   繁体   English

如何获得没有特定相关实体的所有实体?

[英]How can I get all entities without a specific related entity?

I have these tables: 我有这些表:

users 用户

id 
name 

events 事件

id 
name

entries

id
user_id
event_id

How can I get all users that do not have an entry with event_id 4? 如何获得所有没有event_id 4条目的用户?

With this code: 使用此代码:

 $users = User::query();
 $users->leftJoin('entries', 'users.id', '=', 'entries.user_id')
       ->where('event_id','!=', $event_id)->get();

I still get users that already have an entry for that specific event. 我仍然得到已经有该特定事件条目的用户。

This is what I want: 这就是我要的:

  • Get all entries which have event_id 4 获取所有具有event_id 4的条目
  • Get the user_id from those entries 从这些条目中获取user_id
  • Remove other entries which have that user_id. 删除具有该user_id的其他条目。

$entries = Entry::where(event_id, '=', 4)->get();

foreach ($entries as &$entry) {
    //remove all entries from $entries array which "user_id = $entry->user_id"
}

How can I do the above with just a query? 仅凭查询我该如何做?

Going by your question following is the answer but i guess this is not what you finally want. 以下是您提出的问题的答案,但是我想这不是您最终想要的。 so elaborate more and specify the sample input and output datasets 进一步详细说明并指定样本输入和输出数据集

select * from your_table where event_id <> 4;

The SQL you want is: 您想要的SQL是:

select user_id
from t 
group by user_id
having sum(event_id = 4) = 0;
select u.*
  from User u
  left join entries e
  on e.user_id = u.id and e.event_id = 4
  where e.id is null

You're looking for <> , not != . 您在寻找<> ,而不是!= Also you're not letting it know which table event_id is on. 另外,您也不会让它知道哪个表event_id处于打开状态。

Try 尝试

 $users = User::query();
 $users->leftJoin('entries', 'users.id', '=', 'entries.user_id')
       ->where('entries.event_id','<>', $event_id)->get();

More information can be found at https://laravel.com/docs/5.4/queries#where-clauses 可以在https://laravel.com/docs/5.4/queries#where-clauses中找到更多信息。

You can use the whereDoesntHave builder method. 您可以使用whereDoesntHave构建器方法。

$users = User::query()->whereDoesntHave("entry", function($q) use ($event_id) {
    $q->where('id', $event_id);
})->get();

The method takes a closure in which you can define criteria for the related entities that must not exist. 该方法采用闭包形式,您可以在其中为必须不存在的相关实体定义条件。


If you need the users and the entries, I think you can still join the entries using with . 如果您需要用户和条目,我认为您仍然可以使用with加入条目。

$users = User::query()
    ->with('entries')
    ->whereDoesntHave("entry", function($q) use ($event_id) {
        $q->where('id', $event_id); })
    ->get();

暂无
暂无

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

相关问题 如何获得一行以及所有相关行的数量? - How can I get a row with along the number of all related rows? 如何在不将所有表元数据拉入内存的情况下使用 jOOQ 获取特定表的列? - How can I get columns for a specific table using jOOQ without pulling all table metadata into memory? 我如何使用 mysql 查询来获取一个实体和与其相关的多个实体? - How can i use a mysql query to get an entity and multiple entities that relate to it? 如何使用 Entity Framework 和 .NET 5.0 构建查询以在相关表中查找不匹配的实体? - How do I contruct a query to find unmatched entities in related table with Entity Framework and .NET 5.0? 如何在不知道分类深度的情况下如何获取与该分类相关的所有子分类。 - How do I get all sub-categories related to a category without the knowing how deep it goes. 如何在Entity Framework Core 1.0.1原始查询中包含相关实体? - How to include related entities in an Entity Framework Core 1.0.1 raw query? 如何根据条件 laravel 获取左表中的所有数据和相关表数据 - How can i get all data in left table and related table data based on condition laravel Java Hibernate如何保持没有关系的实体列表? - Java hibernate how can I persist List of entities without relationship? 如何选择一行及其所有相关行? - How can I select a row plus all its related rows? 使用linq和相关实体查询实体框架7 - Query to entity framework 7 using linq and related entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM