查询加入条件

发布于 2024-10-08 19:25:21 字数 602 浏览 0 评论 0原文

我有两个名为 account 和signedgroup 的表。在帐户表中有两列名为 ID 和 sourceaccount,在分配组中我有两列名为 ID 和分配组。

我想通过加入帐户和分配组表来选择 ID。我做了这样的事情:

select a.ID
from account a 
  left outer join assignedgroup b 
   on a.sourceaccount = b.assignedgorup

但我得到 Null 作为输出,原因是 Sourceaccount 中的值就像这个

sourceaccount:

sample
sample

而分配组中的值就像这个

分配组:

sample-L1
sample-P-L1

任何人都可以帮助我如何连接这两个表吗?

每当源帐户和分配组中都有样本值时,我想获取所有 ID。

它正在将样本与样本 L1 进行比较,因为它们不相等,所以它返回 null,但我想要 ID,即使它具有这样的值。我的意思是,如果某些列值匹配,那么我也希望显示这些值

I Have two tables named account and assignedgroup. In account table there are two columns named ID and sourceaccount and In assignedgroup I have two columns named ID and assignedgroup.

I want to select ID by joining account and assignedgroup tables. I did something like this:

select a.ID
from account a 
  left outer join assignedgroup b 
   on a.sourceaccount = b.assignedgorup

But I get Null as output the reason being that The values in sourceaccount are like this

sourceaccount:

sample
sample

and whereas the values in assignedgroup are like this

assignedgroup:

sample-L1
sample-P-L1

Can anyone help me on how to join these two tables?

I want to get all the ID's whenever there is a sample value in both sourceaccount and assignedgroup.

It is comparing sample with sample-L1 since those are not equal it is returning null but I want the ID even if it has values like that. I mean if somepart of the column values matches then also I want those values to be displayed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×纯※雪 2024-10-15 19:25:21
SELECT  a.id
FROM    account a
LEFT JOIN
        assignedgroup b
ON      b.assignedgorup LIKE a.sourceaccount + '-%'
SELECT  a.id
FROM    account a
LEFT JOIN
        assignedgroup b
ON      b.assignedgorup LIKE a.sourceaccount + '-%'
﹏半生如梦愿梦如真 2024-10-15 19:25:21

显然你的数据库结构有缺陷。如果您需要分配组的第一部分加入源帐户,则应将其存储在单独的列中。您可以进行一些操作来获取正确的值,但更改数据库结构是最好的解决方案,因为您在联接中执行的任何数据操作都非常昂贵(并且可能会引入错误)。

Clearly your database structure is flawed. If you need the first part of the assigned group to join to the source account, it should be stored that way in a separate column. YOu can do some manipulation to get the right values, but changing the datbase structure is the best fix as any data manipulation you do in a join is very expensive (and likely to introduce bugs).

空城旧梦 2024-10-15 19:25:21

由于这两个表中的实际数据实际上并不匹配,因此您无法连接这些表中的这些行,除非您开始对这些表中数据的性质做出一些相当广泛的假设。例如,这可能有效:

SELECT a.ID
 from Account a
  left outer join AssignedGroup b
   on a.sourceaccount = left(b.assignedgroup, 6)

或者,更一般地说,

SELECT a.ID
 from Account a
  left outer join AssignedGroup b
   on a.sourceaccount = left(b.assignedgroup, len(a.sourceaccount))

但是,这确实是可怕的代码、逻辑和数据库设计,我只会在尝试排除故障和/或修复混乱的数据时使用它。

Since the actual data within those two tables does not actually match, you cannot join those rows in those tables unless you start making some rather broad assumptions about the nature of the data in those tables. For example, this might work:

SELECT a.ID
 from Account a
  left outer join AssignedGroup b
   on a.sourceaccount = left(b.assignedgroup, 6)

or, more generically,

SELECT a.ID
 from Account a
  left outer join AssignedGroup b
   on a.sourceaccount = left(b.assignedgroup, len(a.sourceaccount))

However, this is truly horrible code, logic, and database design, and I would only use this while tyring to troubleshoot and/or fix messed-up data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文