“子查询返回多于 1 行"错误. [英] "subquery returns more than 1 row" error.

查看:31
本文介绍了“子查询返回多于 1 行"错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网络编程的新手,我正在尝试制作 twitter 克隆.此时,我有 3 个表:

I am new to web programming and I'm trying to make a twitter-clone. At this point, I have 3 tables:

用户(ID,姓名)

  • id 是自动生成的 id
  • 用户名
  • 推文(id、内容、user_id)

    tweets (id, content, user_id)

    • id 是自动生成的 id
    • content 是推文的文本
    • user_id 是发布帖子的用户的 ID
    • 关注者(id、user_id、follow_id)

      followers (id, user_id, following_id)

      • id 是自动生成的 id
      • user_id 是执行以下操作的用户
      • following_id 是被关注的用户
      • 因此,作为 sql 的新手,我正在尝试构建一个 SQL 语句,该语句将返回当前登录用户及其关注的每个人的推文.

        So, being new to sql as well, I am trying to build an SQL statement that would return the tweets that the currently-logged in user and of everyone he follows.

        我尝试使用此语句,该语句有时有效,但有时,我收到一条错误消息,指出子查询返回多于 1 行".声明如下:

        I tried to use this statement, which works sometimes, but other times, I get an error that says "Subquery returns more than 1 row". Here is the statement:

        SELECT * FROM tweets 
        WHERE user_id IN
        ((SELECT following_id FROM followers
        WHERE user_id = 1),1) ORDER BY date DESC
        

        • 我在这里以 1 为例,这将是当前登录用户的 id.
        • 我对这个声明没有任何运气;任何帮助将不胜感激!谢谢.

          I haven't had any luck with this statement; any help would be greatly appreciated! Thank you.

          推荐答案

          SELECT * 
          FROM tweets 
          WHERE 
            user_id IN (SELECT following_id FROM followers WHERE user_id = 1)
            OR user_id = 1
          ORDER BY date DESC
          

          这篇关于“子查询返回多于 1 行"错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆