uniek sorteren uit DB
ik heb jullie hulp nodig, de titel zegt eigenlijk al genoeg,
uit een queryresult krijg ik dit (een aanal cijfers bestaande uit 2 karakters):
13
14
16
22
23
25
31
32
33
34
35
36
44
46
Het linkerkarakter mag meedere malen voorkomen, maar het rechterkarakter moet uniek blijven. Hoe is dit (direct) te realiseren in SQL, en anders in php (sort functie)
Dit moet uiteindelijk het resultaat worden van bovenstaande uitvoer:
13
22
25
31
44
46
Gewijzigd op 01/01/1970 01:00:00 door BlcKJcK
volgens mij was het zo uit mn hoofd
BlcKJcK schreef op 15.01.2007 00:59:
Het linkerkarakter mag meedere malen voorkomen, maar het rechterkarakter moet uniek blijven.
Het kan bijna niet anders of je datamodel klopt niet. Waarom wil je dit zo?
Ik ben net als Jan Koehoorn errug benieuwt waarom je dit wilt. Ik kan alvast verklappen dat Jurgens oplossing niet de juiste is.
Input
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
CREATE TABLE `test` (
`id` int(10) unsigned NOT NULL auto_increment,
`field1` varchar(255) default NULL,
`field2` varchar(255) default NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '10', 'Lorem Ipsum 10');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '11', 'Lorem Ipsum 11');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '12', 'Lorem Ipsum 12');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '13', 'Lorem Ipsum 13');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '14', 'Lorem Ipsum 14');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '15', 'Lorem Ipsum 15');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '16', 'Lorem Ipsum 16');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '17', 'Lorem Ipsum 17');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '18', 'Lorem Ipsum 18');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '19', 'Lorem Ipsum 19');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '20', 'Lorem Ipsum 20');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '21', 'Lorem Ipsum 21');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '22', 'Lorem Ipsum 22');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '23', 'Lorem Ipsum 23');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '24', 'Lorem Ipsum 24');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '25', 'Lorem Ipsum 25');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '26', 'Lorem Ipsum 26');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '27', 'Lorem Ipsum 27');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '28', 'Lorem Ipsum 28');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '29', 'Lorem Ipsum 29');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '30', 'Lorem Ipsum 30');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '31', 'Lorem Ipsum 31');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '32', 'Lorem Ipsum 32');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '33', 'Lorem Ipsum 33');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '34', 'Lorem Ipsum 34');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '35', 'Lorem Ipsum 35');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '36', 'Lorem Ipsum 36');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '37', 'Lorem Ipsum 37');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '38', 'Lorem Ipsum 38');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '39', 'Lorem Ipsum 39');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '40', 'Lorem Ipsum 40');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '41', 'Lorem Ipsum 41');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '42', 'Lorem Ipsum 42');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '43', 'Lorem Ipsum 43');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '44', 'Lorem Ipsum 44');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '45', 'Lorem Ipsum 45');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '46', 'Lorem Ipsum 46');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '47', 'Lorem Ipsum 47');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '48', 'Lorem Ipsum 48');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '49', 'Lorem Ipsum 49');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '50', 'Lorem Ipsum 50');
`id` int(10) unsigned NOT NULL auto_increment,
`field1` varchar(255) default NULL,
`field2` varchar(255) default NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '10', 'Lorem Ipsum 10');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '11', 'Lorem Ipsum 11');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '12', 'Lorem Ipsum 12');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '13', 'Lorem Ipsum 13');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '14', 'Lorem Ipsum 14');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '15', 'Lorem Ipsum 15');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '16', 'Lorem Ipsum 16');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '17', 'Lorem Ipsum 17');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '18', 'Lorem Ipsum 18');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '19', 'Lorem Ipsum 19');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '20', 'Lorem Ipsum 20');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '21', 'Lorem Ipsum 21');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '22', 'Lorem Ipsum 22');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '23', 'Lorem Ipsum 23');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '24', 'Lorem Ipsum 24');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '25', 'Lorem Ipsum 25');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '26', 'Lorem Ipsum 26');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '27', 'Lorem Ipsum 27');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '28', 'Lorem Ipsum 28');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '29', 'Lorem Ipsum 29');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '30', 'Lorem Ipsum 30');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '31', 'Lorem Ipsum 31');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '32', 'Lorem Ipsum 32');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '33', 'Lorem Ipsum 33');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '34', 'Lorem Ipsum 34');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '35', 'Lorem Ipsum 35');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '36', 'Lorem Ipsum 36');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '37', 'Lorem Ipsum 37');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '38', 'Lorem Ipsum 38');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '39', 'Lorem Ipsum 39');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '40', 'Lorem Ipsum 40');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '41', 'Lorem Ipsum 41');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '42', 'Lorem Ipsum 42');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '43', 'Lorem Ipsum 43');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '44', 'Lorem Ipsum 44');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '45', 'Lorem Ipsum 45');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '46', 'Lorem Ipsum 46');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '47', 'Lorem Ipsum 47');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '48', 'Lorem Ipsum 48');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '49', 'Lorem Ipsum 49');
INSERT INTO `test` (`id` , `field1` , `field2`) VALUES ('', '50', 'Lorem Ipsum 50');
SQL:
Code (php)
1
2
3
2
3
SELECT DISTINCT substring(`field1`, 2, 1) AS `_char`, id, field1, field2
FROM `test`
GROUP BY `_char`
FROM `test`
GROUP BY `_char`
Resultaat:
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
0 2 10 Lorem Ipsum 10
1 3 11 Lorem Ipsum 11
2 4 12 Lorem Ipsum 12
3 5 13 Lorem Ipsum 13
4 6 14 Lorem Ipsum 14
5 7 15 Lorem Ipsum 15
6 8 16 Lorem Ipsum 16
7 9 17 Lorem Ipsum 17
8 10 18 Lorem Ipsum 18
9 11 19 Lorem Ipsum 19
1 3 11 Lorem Ipsum 11
2 4 12 Lorem Ipsum 12
3 5 13 Lorem Ipsum 13
4 6 14 Lorem Ipsum 14
5 7 15 Lorem Ipsum 15
6 8 16 Lorem Ipsum 16
7 9 17 Lorem Ipsum 17
8 10 18 Lorem Ipsum 18
9 11 19 Lorem Ipsum 19
Gewijzigd op 01/01/1970 01:00:00 door Martijn Wieringa