【问题标题】:Split table row into two fields and count each将表格行拆分为两个字段并计算每个字段
【发布时间】:2018-05-19 17:59:21
【问题描述】:

到目前为止,我已经编写了以下查询:

SELECT forum_topics.*, users.id as userid, users.username, users.avatar, forum_categories.name as cat_name
FROM forum_topics 
INNER JOIN users
ON users.id = forum_topics.author_id
INNER JOIN forum_categories
ON forum_categories.id = forum_topics.category_id
WHERE forum_topics.id = 64

但我还想添加另一个表votes,其结构如下:

___________________________________________________________
| id | object_type | object_id | receiver | giver | type    |
___________________________________________________________
| 128| topic       |     64    |    21    |  22   | like    |
| 129| topic_reply |     55    |    21    |  22   | dislike |
___________________________________________________________

基本上这两个表之间的关系是表 1 中的 forum_topics.id 和表 2 中的 object_id(底部)。这是一个论坛,我想显示每个主题的喜欢/不喜欢并回复。 type 可以是 likedislikereceiver 是发帖的用户,giver 是投票的用户。我想在第一个查询中 INNER JOIN 表 votes 并将所有喜欢和不喜欢的人统计到两个单独的字段中。类似的东西:

Select votes.count(*) as likes WHERE type = 'like and votes.count(*) as dislikes WHERE type = 'dislike' 查询变得如此复杂,我很困惑。

编辑:所以我想出了forum_topics。这是我的做法:

    SELECT forum_topics.*, users.id as userid, users.username, users.avatar, forum_categories.name as cat_name,
   count(CASE WHEN votes.type = 'like'    AND votes.object_type = 'topic'  then 1 else null end) as votes_likes, 
   count(CASE WHEN votes.type = 'dislike' AND votes.object_type = 'topic'  then 1 else null end) as votes_dislikes
FROM forum_topics 
INNER JOIN users
ON users.id = forum_topics.author_id
INNER JOIN forum_categories
ON forum_categories.id = forum_topics.category_id
INNER JOIN votes 
ON votes.object_id = forum_topics.id
WHERE forum_topics.id = ?

现在 forum_posts 不工作..

SELECT forum_posts.*, users.id as userid, users.username, users.avatar,
      count(CASE WHEN votes.type = 'like'    AND votes.object_type = 'topic_post' then 1 else null end) as votes_likes, 
      count(CASE WHEN votes.type = 'dislike' AND votes.object_type = 'topic_post' then 1 else null end) as votes_dislikes
  FROM forum_posts
  INNER JOIN users
  ON users.id = forum_posts.author_id
  LEFT JOIN votes 
  ON votes.object_id = forum_posts.id
  WHERE forum_posts.topic_id = 64
  ORDER BY forum_posts.id

任何想法如何解决它?在 HeidiSQL 中,它返回一行,所有内容均为 NULL。

【问题讨论】:

  • 您可能不想INNER JOIN 投票表,因为这将删除任何没有投票的forum_topics 记录。
  • 我想我设法使它适用于forum_topics,但正如你所说的forum_posts 一切都消失了。我编辑了我的帖子。请问可以查一下吗?
  • 能否提供forum_posts和forum_topics的结构?另外,投票表中的 object_type 和 object_id 之间的关系是什么?一个object_id可以在投票表中有多个object_types吗?
  • forum_posts forum_topics 这是结构。不,没有其他相关值。 topic 是主题,topic_posts 是回复。

标签: mysql


【解决方案1】:

你需要一个GROUP BY

SELECT forum_posts.id
       , forum_posts.author_id
       , forum_posts.editor_id
       , forum_posts.topic_id
       , forum_posts.content
       , forum_posts.date_created
       , forum_posts.updated
       , users.id as userid
       , users.username
       , users.avatar
       , count(CASE WHEN votes.type = 'like' AND votes.object_type = 'topic_post' THEN 1 ELSE NULL END) AS votes_likes
       , count(CASE WHEN votes.type = 'dislike' AND votes.object_type = 'topic_post' THEN 1 ELSE NULL END) AS votes_dislikes
FROM forum_posts
INNER JOIN users ON users.id = forum_posts.author_id
LEFT JOIN votes ON votes.object_id = forum_posts.id
WHERE forum_posts.topic_id = 64
GROUP BY forum_posts.id
       , forum_posts.author_id
       , forum_posts.editor_id
       , forum_posts.topic_id
       , forum_posts.content
       , forum_posts.date_created
       , forum_posts.updated 
       , users.id
       , users.username
       , users.avatar

【讨论】:

    【解决方案2】:

    尝试使用分组方式;

    SELECT type, COUNT(*) 
    FROM votes 
    GROUP BY type;
    

    【讨论】:

    • 你能给我举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2019-07-13
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多