如何对 NULL 字段进行分组

发布于 2024-12-08 03:24:19 字数 978 浏览 0 评论 0原文

在这个查询之后:

SELECT forum_categories.id AS category_id, forum_categories.title, forum_topics.id AS topic_id, forum_topics.title, user
FROM forum_categories JOIN forum_topics ON forum_categories.id=forum_topics.category_id
LEFT OUTER JOIN forum_views ON forum_topics.id=forum_views.topic_id
WHERE forum_categories.id=6
ORDER BY forum_categories.id

我得到了这个结果:

在此处输入图像描述

现在,我想:

  • 在选择名为 thereIsANull
  • 按字段 user category_id
  • 设置 thereIsANull 对我的所有结果进行分组到如果 user 字段中不存在 NULL(分组时),则为 1,否则为 0。

因此,在下面的示例中,结果必须为 1 行:

6    Welcome    (some topic_id)     (some title)   (djfonplaz or null)   0

并且,如果所有用户都不同于 NULL:

6    Welcome    (some topic_id)     (some title)   (djfonplaz)   1

我该如何在 MySql 上执行此操作?

After this query :

SELECT forum_categories.id AS category_id, forum_categories.title, forum_topics.id AS topic_id, forum_topics.title, user
FROM forum_categories JOIN forum_topics ON forum_categories.id=forum_topics.category_id
LEFT OUTER JOIN forum_views ON forum_topics.id=forum_views.topic_id
WHERE forum_categories.id=6
ORDER BY forum_categories.id

I got this results :

enter image description here

Now, I'd like to :

  • add another field in the select, called thereIsANull
  • group all my results by the field user category_id
  • setting thereIsANull to 1 if there isn't a NULL in the field user (while grouping), 0 otherwise.

So in the example below, the result must be 1 row :

6    Welcome    (some topic_id)     (some title)   (djfonplaz or null)   0

and, if all user was different from NULL :

6    Welcome    (some topic_id)     (some title)   (djfonplaz)   1

How can I do it on MySql?

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

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

发布评论

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

评论(3

追星践月 2024-12-15 03:24:19
SELECT 
  forum_categories.id AS category_ids
  , forum_categories.title as titles
  , forum_topics.id AS topic_ids
  , forum_topics.title as topic_titles
  , count(*) as NumberOfRows
  , GROUP_CONCAT(IFNULL(user,'(no user)')) AS users 
  , (count(*) - count(user)) as IsThereANull  
FROM forum_categories 
INNER JOIN forum_topics ON forum_categories.id=forum_topics.category_id
LEFT OUTER JOIN forum_views ON forum_topics.id=forum_views.topic_id
WHERE forum_categories.id=6
GROUP BY category_id
ORDER BY forum_categories.id

如果不存在 null,则 IsThereANull 将为 0;如果存在,则为 1 或更多。

SELECT 
  forum_categories.id AS category_ids
  , forum_categories.title as titles
  , forum_topics.id AS topic_ids
  , forum_topics.title as topic_titles
  , count(*) as NumberOfRows
  , GROUP_CONCAT(IFNULL(user,'(no user)')) AS users 
  , (count(*) - count(user)) as IsThereANull  
FROM forum_categories 
INNER JOIN forum_topics ON forum_categories.id=forum_topics.category_id
LEFT OUTER JOIN forum_views ON forum_topics.id=forum_views.topic_id
WHERE forum_categories.id=6
GROUP BY category_id
ORDER BY forum_categories.id

IsThereANull will be 0 is there's no null and 1 or more if there is.

故事↓在人 2024-12-15 03:24:19

我认为以下查询应该得到您正在寻找的结果:

SELECT forum_categories.id AS category_id, forum_categories.title, 
       forum_topics.id AS topic_id, forum_topics.title, user, 
       (
          SELECT MAX(IF(COUNT(*), 0, 1))
          FROM forum_categories JOIN forum_topics ON 
               forum_categories.id=forum_topics.category_id
          LEFT OUTER JOIN forum_views ON forum_topics.id=forum_views.topic_id
          WHERE forum_categories.id=6 AND user IS NULL
    ) AS thereIsANull
FROM forum_categories JOIN forum_topics ON forum_categories.id=forum_topics.category_id
LEFT OUTER JOIN forum_views ON forum_topics.id=forum_views.topic_id
WHERE forum_categories.id=6 AND user IS NOT NULL
GROUP BY user
ORDER BY forum_categories.id

I think that the following query should get the result that you are looking for:

SELECT forum_categories.id AS category_id, forum_categories.title, 
       forum_topics.id AS topic_id, forum_topics.title, user, 
       (
          SELECT MAX(IF(COUNT(*), 0, 1))
          FROM forum_categories JOIN forum_topics ON 
               forum_categories.id=forum_topics.category_id
          LEFT OUTER JOIN forum_views ON forum_topics.id=forum_views.topic_id
          WHERE forum_categories.id=6 AND user IS NULL
    ) AS thereIsANull
FROM forum_categories JOIN forum_topics ON forum_categories.id=forum_topics.category_id
LEFT OUTER JOIN forum_views ON forum_topics.id=forum_views.topic_id
WHERE forum_categories.id=6 AND user IS NOT NULL
GROUP BY user
ORDER BY forum_categories.id
沉溺在你眼里的海 2024-12-15 03:24:19

我认为这样的事情会如您所愿:

SELECT cat.id AS category_id, 
       cat.title, 
       top.id AS topic_id, 
       top.title, 
       user,
       (
           (
               SELECT COUNT(*) FROM forum_categories 
               WHERE id = cat.id AND user IS NULL
           ) > 0 -- This is to check wether if any NULL is found
       ) as thereIsANull 
FROM forum_categories as cat
JOIN forum_topics as top ON cat.id = top.category_id
LEFT OUTER JOIN forum_views as view ON top.id = view.topic_id
WHERE cat.id = 6 -- You can ofcourse change this to whatever variable your using
GROUP BY cat.id -- Just an ordinary group by category id
ORDER BY cat.id

假设在forum_categories表中找到了字段用户,否则只需修改子查询以加入您从中获取用户的表

请注意,Thiis基本上与< a href="https://stackoverflow.com/users/263671/cez">Cez 写道,所以他值得一些功劳,唯一的区别是子查询和按类别 id 而不是用户分组。

尝试一下,让我知道它是否正确:)

I think something like this would do as you want:

SELECT cat.id AS category_id, 
       cat.title, 
       top.id AS topic_id, 
       top.title, 
       user,
       (
           (
               SELECT COUNT(*) FROM forum_categories 
               WHERE id = cat.id AND user IS NULL
           ) > 0 -- This is to check wether if any NULL is found
       ) as thereIsANull 
FROM forum_categories as cat
JOIN forum_topics as top ON cat.id = top.category_id
LEFT OUTER JOIN forum_views as view ON top.id = view.topic_id
WHERE cat.id = 6 -- You can ofcourse change this to whatever variable your using
GROUP BY cat.id -- Just an ordinary group by category id
ORDER BY cat.id

Assuming that the field user is found in the forum_categories table, other wise just modify the subquery to join that table where you get the user from

Note that Thiis is basically the same as the one Cez wrote, so he deserves some credits to, only difference is the subquery and grouping by category id instead of user.

Try it out and let me know if it was correct :)

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