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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| --------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE IF NOT EXISTS `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned not null auto_increment primary key,
`tableau` varchar(255) not null,
`vote` enum('oui','non')
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`tableau`,`vote`) values
('un','non'),('un','oui'),('un','oui'),('un','oui'),
('deux','non'),('deux','non'),('deux','non'),('deux','oui'),
('trois','non'),('trois','oui'),('trois','oui'),('trois','non'),
('quatre','non'),('quatre','oui'),('quatre','oui'),('quatre','non'),('quatre','non'),('quatre','non')
--------------
--------------
select * from `test`
--------------
+----+---------+------+
| id | tableau | vote |
+----+---------+------+
| 1 | un | non |
| 2 | un | oui |
| 3 | un | oui |
| 4 | un | oui |
| 5 | deux | non |
| 6 | deux | non |
| 7 | deux | non |
| 8 | deux | oui |
| 9 | trois | non |
| 10 | trois | oui |
| 11 | trois | oui |
| 12 | trois | non |
| 13 | quatre | non |
| 14 | quatre | oui |
| 15 | quatre | oui |
| 16 | quatre | non |
| 17 | quatre | non |
| 18 | quatre | non |
+----+---------+------+
--------------
select concat('array(', group_concat(elem separator ','), ')') as ''
from ( select concat('array("',tableau, '", ',sum(case vote when 'oui' then 1 else 0 end), ', ',sum(case vote when 'non' then 1 else 0 end),')') as elem
from `test`
group by tableau
) as x
--------------
+-----------------------------------------------------------------------------------------+
| |
+-----------------------------------------------------------------------------------------+
| array(array("un", 3, 1),array("deux", 1, 3),array("trois", 2, 2),array("quatre", 2, 4)) |
+-----------------------------------------------------------------------------------------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |