mysql group by、字段查询

发布于 2025-01-01 06:13:16 字数 3414 浏览 1 评论 0原文

用非常简单的术语解释一下,我有以下几项:

-------------------------------------------------------------------------------
id  Title                              unique_id  type  language  category       
-------------------------------------------------------------------------------
1   Announcement One                        111     1   1         5,6,8
2   Announcement Two                        112     1   1         6
3   Announcement Three                      113     1   1         7
4   Ankundigung Drei                        113     1   2         7,3
5   Announcement Four                       115     1   1         6
6   Announcement of another type            116     2   1         5
7   Another announcement of another type    117     2   1         7
8   Ein anderes ankundigung                 117     2   2         7
9   Subtype 3                               118     3   1         4
10  Tip 3                                   118     3   2         4
-------------------------------------------------------------------------------

这里的关键字段是unique_id(这是唯一的公告ID,而不是主ID)。

公告列表是英语的(语言=1),偶尔也会有德语的公告(语言=2) - 就像 unique_id 113,117 和 118 的情况一样。

我想做的是英语用户看到英文版本公告,德国用户也可以看到英文版本的公告,因为德国用户也默认使用英语。但是,只有在公告还有德语版本的情况下,该版本才应以德语而非英语向德国用户显示。

我认为我可以通过简单的 GROUP BY unique_id 和语言(asc 或 desc)来实现此目的,但这会引发非常不可预测的结果,并且显然不起作用。

另外,我想按指定类别对结果进行排序,例如使用:field(类别,7,6,2),然后按升序对其余类别进行排序。对于类别,如果可能的话,我想使用 contains,因为一个公告可能有多个类别,在这种情况下我认为必须使用 contains 来排序。所以,德国用户应该看到以下结果:

-------------------------------------------------------------------------------
id  Title                              unique_id  type  language  category       
-------------------------------------------------------------------------------
1   Ankundigung Drei                        113     1   2         7,3
2   Ein anderes ankundigung                 117     2   2         7
3   Announcement One                        111     1   1         5,6,8
4   Announcement Two                        112     1   1         6
5   Announcement Four                       115     1   1         6
6   Tip 3                                   118     3   2         4
7   Announcement of another type            116     2   1         5
-------------------------------------------------------------------------------

这是我第一次在 stackoverflow 上发布问题,希望我发布了一个好的问题:)

提前致谢

完整的 SQL 下面:

-- Table structure for table `anns`
--

CREATE TABLE IF NOT EXISTS `anns` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(254) NOT NULL,
  `unique_id` int(11) NOT NULL,
  `type` int(11) NOT NULL,
  `language` int(11) NOT NULL,
  `category` varchar(254) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `anns`
--

INSERT INTO `anns` (`id`, `title`, `unique_id`, `type`, `language`, `category`) VALUES
(1, 'Announcement One', 111, 1, 1, '2,6,8'),
(2, 'Announcement Two', 112, 1, 1, '6'),
(3, 'Announcement Three', 113, 1, 1, '7'),
(4, 'Ankundigung Drei', 113, 1, 2, '7'),
(5, 'Announcement Five', 115, 1, 1, '6'),
(6, 'Announcement of another type', 116, 2, 1, '6'),
(7, 'Another announcement of another type', 117, 2, 1, '7'),
(8, 'Ein anderes ankundigung', 117, 2, 2, '7'),
(9, 'Subtype 3', 118, 3, 1, '4'),
(10, 'Tip 3', 118, 3, 2, '4');   

to explain it in very simple terms, I have the following items:

-------------------------------------------------------------------------------
id  Title                              unique_id  type  language  category       
-------------------------------------------------------------------------------
1   Announcement One                        111     1   1         5,6,8
2   Announcement Two                        112     1   1         6
3   Announcement Three                      113     1   1         7
4   Ankundigung Drei                        113     1   2         7,3
5   Announcement Four                       115     1   1         6
6   Announcement of another type            116     2   1         5
7   Another announcement of another type    117     2   1         7
8   Ein anderes ankundigung                 117     2   2         7
9   Subtype 3                               118     3   1         4
10  Tip 3                                   118     3   2         4
-------------------------------------------------------------------------------

The key field here is unique_id (which is a unique announcement id, not primary id).

The announcement list is in English (Language=1) and occasionally there are announcements ALSO in German(Language=2) - as is the case with unique_id 113,117 and 118.

What I want to do is the English users to see the English version of announcements, and German users to see the English version of announcements as well, because English is default for German users as well. However, only in cases where there is ALSO a German version of the announcement, that version should be displayed for German users in German and not in English.

I thought I could achieve this with a simple GROUP BY unique_id and language (asc or desc), but this is throwing very unpredictable results and obviously not working.

Also, I would like to order the results by specified categories, using for example: field (category, 7,6,2) and then the rest of categories to be ordered in ascending order. For categories, if possible, I want to use contains, because one announcement may have several categories in which case I believe contain has to be used for sorting. So, the German user should see the following result:

-------------------------------------------------------------------------------
id  Title                              unique_id  type  language  category       
-------------------------------------------------------------------------------
1   Ankundigung Drei                        113     1   2         7,3
2   Ein anderes ankundigung                 117     2   2         7
3   Announcement One                        111     1   1         5,6,8
4   Announcement Two                        112     1   1         6
5   Announcement Four                       115     1   1         6
6   Tip 3                                   118     3   2         4
7   Announcement of another type            116     2   1         5
-------------------------------------------------------------------------------

This is the first time I'm posting a question on stackoverflow, hopefully I posted a good one :)

Thanks in advance

Full SQL Below:

-- Table structure for table `anns`
--

CREATE TABLE IF NOT EXISTS `anns` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(254) NOT NULL,
  `unique_id` int(11) NOT NULL,
  `type` int(11) NOT NULL,
  `language` int(11) NOT NULL,
  `category` varchar(254) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `anns`
--

INSERT INTO `anns` (`id`, `title`, `unique_id`, `type`, `language`, `category`) VALUES
(1, 'Announcement One', 111, 1, 1, '2,6,8'),
(2, 'Announcement Two', 112, 1, 1, '6'),
(3, 'Announcement Three', 113, 1, 1, '7'),
(4, 'Ankundigung Drei', 113, 1, 2, '7'),
(5, 'Announcement Five', 115, 1, 1, '6'),
(6, 'Announcement of another type', 116, 2, 1, '6'),
(7, 'Another announcement of another type', 117, 2, 1, '7'),
(8, 'Ein anderes ankundigung', 117, 2, 2, '7'),
(9, 'Subtype 3', 118, 3, 1, '4'),
(10, 'Tip 3', 118, 3, 2, '4');   

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

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

发布评论

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

评论(2

夏日浅笑〃 2025-01-08 06:13:17
( SELECT Title,unique_id,type,language,category
    FROM anns 
    WHERE language=2
  UNION
  SELECT Title,unique_id,type,language,category
    FROM anns a 
    WHERE language=1 
      AND NOT EXISTS( SELECT 1 FROM anns a2 
                      WHERE a2.unique_id=a.unique_id 
                        AND a2.language=2 )
) ORDER BY 2

这将选择德语元素和没有德语元素的英语元素。

( SELECT Title,unique_id,type,language,category
    FROM anns 
    WHERE language=2
  UNION
  SELECT Title,unique_id,type,language,category
    FROM anns a 
    WHERE language=1 
      AND NOT EXISTS( SELECT 1 FROM anns a2 
                      WHERE a2.unique_id=a.unique_id 
                        AND a2.language=2 )
) ORDER BY 2

This select the german elements and the english elements where there are no german elements.

翻了热茶 2025-01-08 06:13:16

这可能有助于检索德国用户的结果:

SELECT *
FROM (
    SELECT * 
    FROM  `anns` 
    ORDER BY  `announcement_id` ,  `language` DESC
) `TA`
GROUP BY `TA`.`announcement_id`;

我没有添加基于类别的排序,因为我不确定其行为。但也许您可以尝试在 GROUP BY 之后添加 ORDER BY,例如:

ORDER BY `TA`.`category` DESC"

希望有帮助!

This might help for retrieving results for German users:

SELECT *
FROM (
    SELECT * 
    FROM  `anns` 
    ORDER BY  `announcement_id` ,  `language` DESC
) `TA`
GROUP BY `TA`.`announcement_id`;

I haven't added the sorting based on categories because I'm not sure on the behaviour. But perhaps you may try putting an ORDER BY after the GROUP BY, like:

ORDER BY `TA`.`category` DESC"

Hope it helps!

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