【问题标题】:Writing SQL Query to retrieve data via a Subquery?编写 SQL 查询以通过子查询检索数据?
【发布时间】:2015-08-03 05:37:18
【问题描述】:

我需要编写一个查询来检索符合以下条件的值,

从 UserTable 中选择我 (me@gmail.com) 不是朋友的用户

这是我的表格格式:

我的电子邮件在 UserID 中注明,我的朋友在 FriendID 栏中注明。我需要从 UserTable 中选择未在 FriendsTable 中列为朋友的用户。似乎是一个简单的查询,但我无法弄清楚。这是我尝试过的:

PS:我只是为了澄清而写了这个,因此我不想想写一个参数化查询。我不打算分发这个。

SELECT * From UserTable  WHERE Email NOT LIKE '% (Select FriendsTable.FriendID From FriendsTable Where FriendsTable.UserID='me@gmail.com') %'  

编辑

jpw 的查询成功了。但是,我如何从他的解决方案的查询中检索一个随机行?

这不起作用:

select TOP 1 * from UserTable where UserTable.Email <> '" + email + "' and Email not in (select case when FriendsTable.UserID = '" + email + "' then FriendsTable.FriendID else UserID end from FriendsTable where '" + email + "' in (UserID, FriendID)); ORDER BY NEWID()

【问题讨论】:

    标签: sql azure-sql-database


    【解决方案1】:

    如果朋友关系可以双向,并且您不仅要排除 email = 'me@gmail.com' 的行,还要排除 FriendID = 'me@gmail.com' 的行,即下面的两行

    UserID            FriendID
    me@gmail.com      ddaabb@gmail.com
    kk@gmail.com      me@gmail.com  
    

    那么这个查询会这样做:

    select * 
    from userTable
    where Email <> 'me@gmail.com'
      and Email not in (
        select 
          case 
           when UserID = 'me@gmail.com' 
           then FriendID else UserID 
          end 
        from FriendsTable 
        where 'me@gmail.com' in (UserID, FriendID)
      );
    

    即使您只想排除出现在 FriendID 列中的那些用户,这仍然有效,尽管在这种情况下有更好的方法。

    使用您的示例数据,结果将是:

    kk@gmail.com
    yybb@gmail.com
    

    或者你这样做:

    select * from
    UserTable u
    where u.Email <> 'me@gmail.com' and
    not exists (
        select 1 from FriendsTable
        where FriendID = u.Email
      );
    

    【讨论】:

    • 不,伙计,我只想吸引我不关注的人 :) 不是双向
    • 顺便说一句,我如何从结果中排除我自己的电子邮件?您的解决方案能否返回我自己的电子邮件,因为它不作为朋友存在?我需要排除我的电子邮件
    • 完美!我对您的查询有最后一点澄清。你能检查我的问题编辑吗?
    • SELECT TOP 1 * FROM USERTABLE U WHERE U.EMAIL &lt;&gt; 'ME@GMAIL.COM' AND NOT EXISTS (SELECT 1 FROM FRIENDSTABLE WHERE FRIENDID = U.EMAIL) ORDER BY NEWID();
    • @Earthling 您编辑的查询不起作用的唯一原因是在 order by 之前有一个分号 ;。删除它,你应该没事。
    【解决方案2】:

    如果您正在寻找不是单个用户朋友的所有用户,请尝试以下操作:

    select t.Email
    from UserTable as t
    left join FriendsTable as f on f.UserID=t.Email
    where f.UserID IS NULL and t.Email='me@gmail.com'
    

    【讨论】:

    • 这并没有返回任何用户,伙计
    • 那是因为 me@gmail.com 在 FriendsTable 中。在查询的最后 t.Email='me@gamil.com' 部分中尝试 UserTable 中不是 FriendsTable 的其他用户以获得正确答案。
    【解决方案3】:

    您可以使用NOT EXISTS 来检查Email 是否不存在于friends 表中,如下所示

    select * from
    UserTable ut
    where not exists (
    select 1 from FriendsTable
    where UserID != ut.Email)
    and ut.Email = 'me@gmail.com';
    

    另一种解决方案是使用LEFT JOIN 并选择NULL 喜欢

    select ut.*
    from UserTable ut
    left join FriendsTable ft on ut.Email = ft.UserID
    where ft.UserID is null and ut.Email = 'me@gmail.com';
    

    【讨论】:

    • 所以 ut.Email 会是 me@gmail.com?
    • @Earthling,它将选择所有在朋友表中不存在电子邮件的记录。
    • 我应该在哪里添加“me@gmail.com”?
    • 返回错误:无法绑定多部分标识符“UserTable.UserID”。
    • WHERE 子句中添加另一个条件。如果有帮助,请参阅答案中的编辑。
    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多