【问题标题】:How to retrieve data from sql/Mysql Query?如何从 sql/Mysql 查询中检索数据?
【发布时间】:2024-01-16 10:54:02
【问题描述】:

我想从 sql 查询中检索数据,但我无法正确检索数据。

我有两张桌子:

表用户帖子:

id       userID     postText         status
1        abc        hello world!     0
2        xyz        hello Test!      0
3        abc        hello Que!       0
4        qwe        hello All!       0
5        abc        hello Post!      0
6        rty        hello RTY!       0
7        jkl        hello JKL!       0
8        jkl        hello JKL2!      0
9        jkl        hello JKL3!      0
10       jkl        hello JKL4!      0

表用户关注:

id       followerID     followedID         status
1        abc            xyz                 1
2        xyz            qwe                 1
3        abc            rty                 1
4        qwe            abc                 1
5        abc            jkl                 1

我想编写一个查询来从用户和关注的用户中选择帖子

(USER 和 User 的关注者都发布 LIKE 'abc' 以及所有被 'abc' 关注的帖子)

在哪里 UserPost.Status = 0 AND UserFollow.status = 1

我试过这个但失败了:

SELECT * FROM UserPost INNER JOIN UserFollow 
  ON UserPost.userID = UserFollow.followedID 
    WHERE UserFollow.followerID = 'abc' 
      AND UserFollow.status=1 
        AND UserPost.status=0

// This is not working

我尝试了另一个查询:

select p.*, u.* from ( select * from UserPost 
 where userID = 'abc'  or userID in (select followedID 
 from UserFollow where followedID = 'abc') ) p inner join UserFollow u 
 on u.followedID = p.userID order by p.id
// This query is also not working

我做错了什么?

【问题讨论】:

  • 我已经删除了那些冲突的 dbms 标签。将其中一个放回去,用于实际使用的 dbms。
  • 样表数据很棒,还要指定预期的结果。
  • 请在代码问题中给出minimal reproducible example--cut & paste & runnable code,包括最小的代表性示例输入作为代码;期望和实际输出(包括逐字错误消息);标签和版本;明确的规范和解释。给出尽可能少的代码,即您显示的代码可以通过您显示的代码扩展为不正常的代码。 (调试基础。)对于包含 DBMS 和 DDL(包括约束和索引)和输入为格式化为表的代码的 SQL。 How to Ask 暂停总体目标的工作,将代码砍到第一个表达式,没有给出你期望的内容,说出你期望的内容和原因。
  • 指定预期结果,就像您对示例数据所做的那样。
  • 请通过编辑而非 cmets 进行澄清。将需要的内容放入您的帖子中,而不仅仅是在某个链接上。请use text, not images/links, for text--including tables & ERDs。仅将图像用于无法表达为文本或增强文本的内容。在图像中包含图例/键和说明。使用编辑功能插入图片/链接。

标签: sql subquery inner-join where-clause


【解决方案1】:

如果我理解正确,你可以使用exists

select p.*
from userpost p
where 
    p.status = 0
    and (
        p.userid = 'abc'
        or exists (
            select 1
            from userfollow f
            where f.follower_id = 'abc' and f.followedid = p.userid and f.status = 1
        )
    )

【讨论】:

  • @SoniyaJain:只需将 select p.* 替换为 select count(*)
  • 你是一个真正的天才,一个真正的英雄。非常感谢您的大力帮助!
  • 欢迎@SoniyaJain。如果我的回答正确回答了您的问题,那么请点击复选标志accept it...谢谢。
【解决方案2】:

有几种方法可以做到这一点,但以下是非常直观的。

SELECT * 
FROM UserPost 
where UserPost.status = 0
and (userID = 'abc'
      or UserID in (select followedID
                    from UserFollow
                    where followerID = 'abc'
                    AND status=1)) 

返回

id  userid  posttext    status
1   abc     hello world!    0
2   xyz     hello Test!     0
3   abc     hello Que!      0
5   abc     hello Post!     0
6   rty     hello RTY!      0
7   jkl     hello JKL!      0
8   jkl     hello JKL2!     0
9   jkl     hello JKL3!     0
10  jkl     hello JKL4!     0

【讨论】: