MySQL如何从两个表中选择WHERE NOT EXISTS,同时包括描述的名称。

huangapple go评论65阅读模式
英文:

MySQL How to SELECT WHERE NOT EXISTS from two table along with the name of description

问题

以下是翻译的内容:

我有两个表格,需要选择在log_k_b中不存在的用户,以及kb描述的名称。

表格:user

id deleted user_name first_name last_name
1 0 admin admin admin
5ee 0 agent01 agent1 agent
3fe 0 agent02 agent2 agent
4ff 0 agent03 agent3 agent

表格:log_k_b

id k_b_id deleted description created_by_id created_at
1 5fe18ef2425a093ea 0 Program Provider 5ee 2023-03-08 03:25:16
2 5fe18ef2425a093ea 0 Program Provider 5ee 2023-03-08 03:33:57

这是查询的结果

userId userName k_b_id description created_by_id
1 admin [NULL] [NULL] [NULL]
3fe agent02 [NULL] [NULL] [NULL]
4ff agent03 [NULL] [NULL] [NULL]

但是结果应该看起来像这样

userId userName k_b_id description created_by_id
1 admin 5fe18ef2425a093ea Program Provider 5ee
3fe agent02 5fe18ef2425a093ea Program Provider 5ee
4ff agent03 5fe18ef2425a093ea Program Provider 5ee

我尝试从未创建KB的用户中查询结果,同时包括KB描述。

我的查询结构如下(这个示例没有显示任何内容):

select distinct u.id as userId, u.user_name as userName, lkb.name as kbId, lkb.description as kbName
from user u
left join log_k_b lkb on u.id = lkb.created_by_id
where not exists (select u2.id as user_id, lkb2.created_by_id as created_by_id, lkb2.description as kb_name, max(lkb2.created_at) as latest_date
                  from user u2 left join log_k_b lkb2 on u2.id = lkb2.created_by_id
                  where u2.id = lkb2.created_by_id )

希望这可以帮助你。

英文:

I have two table and need to select user who not exist in log_k_b, along with the name of description of kb.

Table: user

id deleted user_name first_name last_name
1 0 admin admin admin
5ee 0 agent01 agent1 agent
3fe 0 agent02 agent2 agent
4ff 0 agent03 agent3 agent

Table: log_k_b

id k_b_id deleted description created_by_id created_at
1 5fe18ef2425a093ea 0 Program Provider 5ee 2023-03-08 03:25:16
2 5fe18ef2425a093ea 0 Program Provider 5ee 2023-03-08 03:33:57

And this is result of query

userId userName k_b_id description created_by_id
1 admin [NULL] [NULL] [NULL]
3fe agent02 [NULL] [NULL] [NULL]
4ff agent03 [NULL] [NULL] [NULL]

But The result should looks like this

userId userName k_b_id description created_by_id
1 admin 5fe18ef2425a093ea Program Provider 5ee
3fe agent02 5fe18ef2425a093ea Program Provider 5ee
4ff agent03 5fe18ef2425a093ea Program Provider 5ee

I am trying to query the result from User who did not create KB along with KB Description

My query is had structure like this (this sample not show anything):

select distinct u.id as userId, u.user_name as userName, lkb.name as kbId, lkb.description as kbName
from user u
left join log_k_b lkb on u.id = lkb.created_by_id
where not exists (select u2.id as user_id, lkb2.created_by_id as created_by_id, lkb2.description as kb_name, max(lkb2.created_at) as latest_date
                  from user u2 left join log_k_b lkb2 on u2.id = lkb2.created_by_id
                  where u2.id = lkb2.created_by_id ) 

答案1

得分: 0

我理解您的问题是:对于每个 KB,请提供未参与其中的用户列表。

以下是一种实现方法:

select u.id, u.user_name, k.k_b_id, k.description
from user u
cross join (select distinct k_b_id, description from log_k_b) k
where not exists (
    select 1 from log_k_b k1 where k1.created_by_id = u.id and k1.k_b_id = k.k_b_id
)

这个想法是生成用户和 KB 的笛卡尔连接,然后使用 not exists 来过滤已经存在于日志表中的元组。这假设给定的 KB 总是具有相同的描述。

通常情况下,您会有一个单独的表来存储 KB,您可以使用它来替代 select distinct 子查询。

英文:

I understand your question as: for each KB, bring the list of users that did not participated it.

Here is one way to do it:

select u.id, u.user_name, k.k_b_id, k.description
from user u
cross join (select distinct k_b_id, description from log_k_b) k
where not exists (
    select 1 from log_k_b k1 where k1.created_by_id = u.id and k1.k_b_id = k.k_b_id
)

The idea is to generate a cartesian join of the users and the KBs; then, we use not exists to filter out tuples that already exist in the log table. This assumes that a given KB always has the same description.

Normally, you would have a separate table to store the KBs, that you would use instead of the select distinct subquery.

huangapple
  • 本文由 发表于 2023年5月17日 16:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定