合并SQL语句

发布于 2024-09-02 23:14:50 字数 514 浏览 1 评论 0原文

我有 3 个表(关注、发帖、用户)

关注有 2 个字段 -> profile_id、following_id

帖子有 3 个字段 -> post_id, profile_id, content

users 有3个字段-> profile_id、first_name、last_name

我想要匹配的 follow.profile_id 值为 1。

当我运行下面的 SQL 语句时,我迈出了获取正确数据的第一步。但是,我现在想要将此结果集的 posts.profile_id 与用户表进行匹配,以便同时显示所有列出的帖子的每个名称(名字和姓氏)。

感谢您的帮助! :)

前任:

  SELECT * 
    FROM follows 
    JOIN postings ON follows.following_id = postings.profile_id 
   WHERE follows.profile_id = 1 

I have 3 tables (follows, postings, users)

follows has 2 fields -> profile_id , following_id

postings has 3 fields -> post_id, profile_id, content

users has 3 fields -> profile_id, first_name, last_name

I have a follows.profile_id value of 1 that I want to match against.

When I run the SQL statement below I get the 1st step in obtaining the correct data. However, I now want to match the postings.profile_id of this resulting set against the users table so each of the names (first and last name) are displayed as well for all the listed postings.

Thank you for your help! :)

Ex:

  SELECT * 
    FROM follows 
    JOIN postings ON follows.following_id = postings.profile_id 
   WHERE follows.profile_id = 1 

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

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

发布评论

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

评论(4

怕倦 2024-09-09 23:14:50

使用:

SELECT * 
    FROM follows 
    JOIN postings ON follows.following_id = postings.profile_id 
    INNER JOIN users on postings.profile_id = users.profile_id
   WHERE follows.profile_id = 1 
ORDER BY tweets.modified DESC

use:

SELECT * 
    FROM follows 
    JOIN postings ON follows.following_id = postings.profile_id 
    INNER JOIN users on postings.profile_id = users.profile_id
   WHERE follows.profile_id = 1 
ORDER BY tweets.modified DESC
◇流星雨 2024-09-09 23:14:50

使用:

  SELECT p.*,
         u.first_name,
         u.last_name
    FROM POSTINGS p
    JOIN FOLLOWS f ON f.following_id = p.profile_id
                  AND f.profile_id = 1
    JOIN USERS u ON u.profile_id = p.profile_id
ORDER BY tweets.modified DESC

Use:

  SELECT p.*,
         u.first_name,
         u.last_name
    FROM POSTINGS p
    JOIN FOLLOWS f ON f.following_id = p.profile_id
                  AND f.profile_id = 1
    JOIN USERS u ON u.profile_id = p.profile_id
ORDER BY tweets.modified DESC
回眸一笑 2024-09-09 23:14:50
SELECT * 
FROM follows, postings, users
WHERE follows.following_id = postings.profile_id 
AND users.profile_id = follows.profile_id
AND follows.profile_id = 1 
ORDER BY tweets.modified DESC 
SELECT * 
FROM follows, postings, users
WHERE follows.following_id = postings.profile_id 
AND users.profile_id = follows.profile_id
AND follows.profile_id = 1 
ORDER BY tweets.modified DESC 
梦冥 2024-09-09 23:14:50

似乎很容易将连接扩展到另一个表...也许我遗漏了问题的某些部分!

    select 
      f.*,
      p.*,
      u.*
    from 

      follows f

          inner join 
      postings p
          on f.following_id = p.profile_id

          inner join
      users u
          on p.profile_id = u.profile_id


    where follows.profile_id = 1

Seems easy enough to extend the join to another table... maybe I am missing some part of the question!

    select 
      f.*,
      p.*,
      u.*
    from 

      follows f

          inner join 
      postings p
          on f.following_id = p.profile_id

          inner join
      users u
          on p.profile_id = u.profile_id


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