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');
This might help for retrieving results for German users:
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:
Hope it helps!