SQL 查询:连接两个单独行中的两个字段

发布于 2024-08-02 06:03:07 字数 806 浏览 8 评论 0原文

我有以下两个表:

USER
FID  UID     VALUE
4    3       John
3    3    Doe
4    4       Jack
3    4       Russel

应该相当清楚,FID 3 = 姓氏,FID 4 = 姓名。

DATEDATA
UID  DATE  
3    1234
4    4321

我想连接这两个表,这样我最终会得到这样的结果:

UID  DATE  NAME   SURNAME
3    1234  John   Doe
4    4321  Jack   Russel

或者... 或者...

UID  DATE  FULLNAME
3    1234  John Doe
4    4321  Jack Russel

有 SQL 专家吗?

这就是我到目前为止所拥有的:

SELECT UID, DATE, VALUE 
    from DATEDATA as D 
    left join USER as U 
    on U.uid = D.uid where fid = 3 OR fid = 4

但这给了我这个:

UID  DATE  VALUE
3    1234  Doe
3    1234  John
4    4321  Russel
4    4321  Jack

有人吗?

I have the following two tables:

USER
FID  UID     VALUE
4    3       John
3    3    Doe
4    4       Jack
3    4       Russel

Should be fairly clear that FID 3 = Surname, and FID 4 = Name.

DATEDATA
UID  DATE  
3    1234
4    4321

I want to join these two tables, so that I end up with something like this:

UID  DATE  NAME   SURNAME
3    1234  John   Doe
4    4321  Jack   Russel

or... alternatively...

UID  DATE  FULLNAME
3    1234  John Doe
4    4321  Jack Russel

Any SQL gurus out there?

This is what I have so far:

SELECT UID, DATE, VALUE 
    from DATEDATA as D 
    left join USER as U 
    on U.uid = D.uid where fid = 3 OR fid = 4

But that gives me this:

UID  DATE  VALUE
3    1234  Doe
3    1234  John
4    4321  Russel
4    4321  Jack

Anyone?

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

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

发布评论

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

评论(4

半窗疏影 2024-08-09 06:03:07
SELECT D.UID, DATE, U.VALUE + ' ' + U2.Value as fullName
from DATEDATA as D 
left join USER as U on U.uid = D.uid and U.fid = 3
left join USER as U2 on U2.uid = D.uid and U2.fid = 4

尽管只要第一个或最后一个为 NULL,这可能会给您一个 NULL 名称。如果您可以接受用户在系统中只有一个名称或另一个名称的情况,那么在这种情况下您可能需要使用 ISNULL 使任一名称成为空字符串。

SELECT D.UID, DATE, U.VALUE + ' ' + U2.Value as fullName
from DATEDATA as D 
left join USER as U on U.uid = D.uid and U.fid = 3
left join USER as U2 on U2.uid = D.uid and U2.fid = 4

Though this could give you a NULL name whenever either first or last is NULL. You may want to use an ISNULL to make either name an empty string in that case if you can accept cases where the user would only have one name or the other in your system.

☆獨立☆ 2024-08-09 06:03:07
SELECT A.UID, DATEDATA.DATE, A.VALUE, B.VALUE from DATEDATA, USER A, USER B 
WHERE A.UID = B.UID AND A.FID = 3 AND B.FID = 4 AND DATEDATA.UID = A.UID
SELECT A.UID, DATEDATA.DATE, A.VALUE, B.VALUE from DATEDATA, USER A, USER B 
WHERE A.UID = B.UID AND A.FID = 3 AND B.FID = 4 AND DATEDATA.UID = A.UID
感受沵的脚步 2024-08-09 06:03:07
select 
  D.UID
, D.DATE
, isnull(U.VALUE, '') 'firstName'
, isnull(U2.VALUE, '') 'surName'
from
  DateData D
left join User U on U.uid = D.uid and U.fid = 3
left join User U2 on U2.uid = D.uid and U2.fid = 4
select 
  D.UID
, D.DATE
, isnull(U.VALUE, '') 'firstName'
, isnull(U2.VALUE, '') 'surName'
from
  DateData D
left join User U on U.uid = D.uid and U.fid = 3
left join User U2 on U2.uid = D.uid and U2.fid = 4
十雾 2024-08-09 06:03:07

您可以使用类似以下内容。它假设您始终有名字。如果您总是有一些字段并且它不是名字,请将其设为第一个字段并重新调整连接。如果您无法保证价值,请告诉我们,我们将提供更困难的解决方案。

select d.uid,COALESCE(d.date,'none entered'),COALESCE(frst.value,'') as NAME,COALESCE(lst.value,'') as SURNAME
from
user frst (NOLOCK)
left join user lst (NOLOCK) on lst.uid=frst.uid
left join datedata d (NOLOCK) on d.uid = frst.uid
where
frst.fid=4
AND lst.fid=3

You can use something like the following. It assumes you always have a first name. If you always have some field and it's not first name, make that the first from and readjust the joins. If you're never guaranteed a value, then let us know and we'll work a harder solution.

select d.uid,COALESCE(d.date,'none entered'),COALESCE(frst.value,'') as NAME,COALESCE(lst.value,'') as SURNAME
from
user frst (NOLOCK)
left join user lst (NOLOCK) on lst.uid=frst.uid
left join datedata d (NOLOCK) on d.uid = frst.uid
where
frst.fid=4
AND lst.fid=3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文