合并 1 列并选择其他

发布于 2024-10-19 01:34:42 字数 869 浏览 1 评论 0原文

我有两个表,一个称为“固定”,一个称为“未读”

“固定”看起来像这样:

+---------+-------+
|pinned_by|post_id|
+---------+-------+
|2        |3      |
+---------+-------+
|2        |5      |
+---------+-------+

“未读”看起来像这样:

+---------+-------+
|unread_by|post_id|
+---------+-------+
|2        |5      |
+---------+-------+
|2        |10     |
+---------+-------+

我想从两个表中选择这个:

+-------+------+------+
|post_id|unread|pinned|
+-------+------+------+
|3      |0     |1     |
+-------+------+------+
|5      |1     |1     |
+-------+------+------+
|10     |1     |0     |
+-------+------+------+

我该怎么做?固定和未读的值可能是 1/0、1/null、true/false 等。我并不关心,只要我能区分哪些 post_id 来自未读、哪些来自固定、哪些来自来自两者。我正在使用MySQL。在本例中,_by 列全部为 2,但在实际实现中会有所不同。我们的想法是, where unread_by=2 和 where pinned_by=2 将以某种方式包含在内。

谢谢

I have two tables, one called "pinned" and one called "unread"

"pinned" looks like this:

+---------+-------+
|pinned_by|post_id|
+---------+-------+
|2        |3      |
+---------+-------+
|2        |5      |
+---------+-------+

"unread" looks like this:

+---------+-------+
|unread_by|post_id|
+---------+-------+
|2        |5      |
+---------+-------+
|2        |10     |
+---------+-------+

I want to select this from the two tables:

+-------+------+------+
|post_id|unread|pinned|
+-------+------+------+
|3      |0     |1     |
+-------+------+------+
|5      |1     |1     |
+-------+------+------+
|10     |1     |0     |
+-------+------+------+

How can I do this? The values that come through for pinned and unread could be 1/0, 1/null, true/false, etc. I don't really care as long as I could differentiate which post_id's came from unread, which came pinned, and which came from both. I'm using MySQL. The _by columns all have 2 in this example, but will vary in the actual implementation. The thought is that a where unread_by=2 and where pinned_by=2 will be included somehow.

Thanks

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

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

发布评论

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

评论(4

面如桃花 2024-10-26 01:34:42

假设有一个 Posts 表,你可以这样做:

Select P.post_Id
    , Max( Case When UR.unread_by Is Not Null Then 1 Else 0 End ) As unread
    , Max( Case When P2.pinned_by Is Not Null Then 1 Else 0 End ) As pinned
From Posts As P
    Left Join Unread As UR
        On UR.post_Id = P.post_Id
    Left Join Pinned As P2
        On P2.post_id = P.post_id
Group By P.post_id

Presuming that there is a table of Posts, you can do:

Select P.post_Id
    , Max( Case When UR.unread_by Is Not Null Then 1 Else 0 End ) As unread
    , Max( Case When P2.pinned_by Is Not Null Then 1 Else 0 End ) As pinned
From Posts As P
    Left Join Unread As UR
        On UR.post_Id = P.post_Id
    Left Join Pinned As P2
        On P2.post_id = P.post_id
Group By P.post_id
贩梦商人 2024-10-26 01:34:42
SELECT COALESCE(pinned.post_id,unread_post_id) AS postid, unread_by, pinned_by 
FROM pinned OUTER JOIN unread ON pinned.post_id = unread.post_id
SELECT COALESCE(pinned.post_id,unread_post_id) AS postid, unread_by, pinned_by 
FROM pinned OUTER JOIN unread ON pinned.post_id = unread.post_id
北方。的韩爷 2024-10-26 01:34:42

使用:

   SELECT x.post_id,
          COUNT(u.unread_by) AS unread,
          COUNT(x.pinned_by) AS pinned
     FROM (SELECT p.post_id
             FROM PINNED p
           UNION
           SELECT u.post_id
             FROM UNREAD u) x
LEFT JOIN UNREAD u ON u.post_id = x.post_id
LEFT JOIN PINNED p ON p.post_id = x.post_id
 GROUP BY x.post_id

Use:

   SELECT x.post_id,
          COUNT(u.unread_by) AS unread,
          COUNT(x.pinned_by) AS pinned
     FROM (SELECT p.post_id
             FROM PINNED p
           UNION
           SELECT u.post_id
             FROM UNREAD u) x
LEFT JOIN UNREAD u ON u.post_id = x.post_id
LEFT JOIN PINNED p ON p.post_id = x.post_id
 GROUP BY x.post_id
有木有妳兜一样 2024-10-26 01:34:42

您可以使用 UNION

SELECT post_id, SUM(PinCount), SUM(UnreadCount)
FROM (
    SELECT post_id, COUNT(*) PinCount, 0 UnreadCount
    FROM pinned
    GROUP BY post_id
    UNION ALL SELECT post_id, 0, COUNT(*) UnreadCount
    FROM unread
    GROUP BY post_id
)
GROUP BY post_id

或者,如果您有一个包含所有帖子的 post 表,您可以使用该表 - 因为 MySql 没有完整的外连接。

SELECT t.post_id, p.PinCount, u.UnreadCount
FROM posts t LEFT JOIN (
    SELECT post_id, COUNT(*) PinCount
    FROM pinned
    GROUP BY post_id
  ) p USING(post_id)
  LEFT JOIN (
    SELECT post_id, COUNT(*) UnreadCount
    FROM unread
    GROUP BY post_id
  ) u USING(post_id)
GROUP BY t.post_id

You can use a UNION:

SELECT post_id, SUM(PinCount), SUM(UnreadCount)
FROM (
    SELECT post_id, COUNT(*) PinCount, 0 UnreadCount
    FROM pinned
    GROUP BY post_id
    UNION ALL SELECT post_id, 0, COUNT(*) UnreadCount
    FROM unread
    GROUP BY post_id
)
GROUP BY post_id

Or, if you have a post table which holds all the posts, you can use that table - since MySql doesn't have a full outer join.

SELECT t.post_id, p.PinCount, u.UnreadCount
FROM posts t LEFT JOIN (
    SELECT post_id, COUNT(*) PinCount
    FROM pinned
    GROUP BY post_id
  ) p USING(post_id)
  LEFT JOIN (
    SELECT post_id, COUNT(*) UnreadCount
    FROM unread
    GROUP BY post_id
  ) u USING(post_id)
GROUP BY t.post_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文