mysql group by,字段查询

时间:2012-01-31 15:33:30

标签: php mysql group-by sql-order-by

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

-------------------------------------------------------------------------------
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),然后按升序排序其余类别。对于类别,如果可能的话,我想使用包含,因为一个公告可能有几个类别,在这种情况下我认为必须包含必须用于排序。因此,德国用户应该看到以下结果:

-------------------------------------------------------------------------------
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');   

2 个答案:

答案 0 :(得分:0)

( 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

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

答案 1 :(得分:0)

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

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"

希望它有所帮助!