将表格行拆分为两个字段并计算每个字段

时间:2017-12-05 23:30:35

标签: mysql

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

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。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试使用group by;

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