我有下表,需要转置,以便我得到一个月的每一天的列
MySQL 5.6架构设置:
CREATE TABLE `products_sold` (
`date` date NOT NULL,
`sold` bigint(41) NOT NULL,
`outgoing` bigint(32) NOT NULL,
`incoming` bigint(32) NOT NULL,
`sellers` int(21) NOT NULL,
`forecast` float(23,15) NOT NULL
);
INSERT INTO `products_sold` (`date`, `sold`, `outgoing`, `incoming`, `forecast`, `sellers`)
VALUES
('2016-01-01', 1571, 2040, 79827, 3568.738769531250000, 41),
('2016-01-01', 7181, 6040, 79827, 3568.738769531250000, 31),
('2016-01-03', 4312, 28110, 79827, 3568.738769531250000, 45),
('2016-01-04', 3747, 26040, 79827, 3568.738769531250000, 51),
('2016-01-05', 5562, 22965, 90523, 2695.571044921875000, 48),
('2016-01-06', 6575, 60795, 311421, 3989.669921875000000, 54),
('2016-01-07', 5914, 40037, 227773, 3921.460693359375000, 51),
('2016-01-08', 9256, 77935, 218118, 3469.458251953125000, 51),
('2016-01-09', 5946, 31055, 92081, 2381.977783203125000, 45),
('2016-01-10', 4469, 17290, 63145, 1177.376464843750000, 34),
('2016-01-11', 4837, 18018, 98986, 3127.859863281250000, 55),
('2016-01-12', 7902, 37288, 189488, 3082.014648437500000, 59),
('2016-01-13', 8186, 46846, 158957, 3532.484863281250000, 61),
('2016-01-14', 7600, 51211, 234417, 3477.270019531250000, 65),
('2016-01-15', 8568, 59820, 229810, 3083.817871093750000, 70),
('2016-01-16', 6931, 43900, 186547, 2112.100097656250000, 44),
('2016-01-17', 5219, 26085, 134130, 967.085449218750000, 42),
('2016-01-18', 4997, 31055, 117833, 3568.440429687500000, 49),
('2016-01-19', 6787, 61045, 247320, 3499.572265625000000, 60),
('2016-01-20', 10875, 56707, 254071, 3992.852050781250000, 76),
('2016-01-21', 8782, 57286, 210270, 3926.461669921875000, 70),
('2016-01-22', 7215, 46310, 202314, 3476.775878906250000, 75),
('2016-01-23', 5167, 26890, 117739, 2390.486816406250000, 49),
('2016-01-24', 3729, 33895, 176456, 1176.403686523437500, 40),
('2016-01-25', 4249, 32450, 142695, 3491.113769531250000, 59),
('2016-01-26', 5529, 33609, 159153, 3374.979492187500000, 58),
('2016-01-27', 6328, 33607, 160742, 3969.424072265625000, 56),
('2016-01-28', 6961, 36075, 158365, 3892.029052734375000, 61),
('2016-01-29', 5792, 37082, 182249, 3426.950683593750000, 65),
('2016-01-30', 5490, 28900, 126754, 2328.356933593750000, 41),
('2016-01-31', 3933, 23832, 110352, 988.326965332031200, 30);
查询1 :
SELECT
`date`
,`sold`
,`outgoing`
,`incoming`
,`sellers`
,`forecast`
FROM products_sold
Results :
| date | sold | outgoing | incoming | sellers | forecast |
|---------------------------|-------|----------|----------|---------|-----------|
| January, 01 2016 00:00:00 | 1571 | 2040 | 79827 | 41 | 3568.7388 |
| January, 01 2016 00:00:00 | 7181 | 6040 | 79827 | 31 | 3568.7388 |
| January, 03 2016 00:00:00 | 4312 | 28110 | 79827 | 45 | 3568.7388 |
| January, 04 2016 00:00:00 | 3747 | 26040 | 79827 | 51 | 3568.7388 |
| January, 05 2016 00:00:00 | 5562 | 22965 | 90523 | 48 | 2695.571 |
| January, 06 2016 00:00:00 | 6575 | 60795 | 311421 | 54 | 3989.67 |
| January, 07 2016 00:00:00 | 5914 | 40037 | 227773 | 51 | 3921.4607 |
| January, 08 2016 00:00:00 | 9256 | 77935 | 218118 | 51 | 3469.4583 |
| January, 09 2016 00:00:00 | 5946 | 31055 | 92081 | 45 | 2381.9778 |
| January, 10 2016 00:00:00 | 4469 | 17290 | 63145 | 34 | 1177.3765 |
| January, 11 2016 00:00:00 | 4837 | 18018 | 98986 | 55 | 3127.8599 |
| January, 12 2016 00:00:00 | 7902 | 37288 | 189488 | 59 | 3082.0146 |
| January, 13 2016 00:00:00 | 8186 | 46846 | 158957 | 61 | 3532.4849 |
| January, 14 2016 00:00:00 | 7600 | 51211 | 234417 | 65 | 3477.27 |
| January, 15 2016 00:00:00 | 8568 | 59820 | 229810 | 70 | 3083.8179 |
| January, 16 2016 00:00:00 | 6931 | 43900 | 186547 | 44 | 2112.1 |
| January, 17 2016 00:00:00 | 5219 | 26085 | 134130 | 42 | 967.08545 |
| January, 18 2016 00:00:00 | 4997 | 31055 | 117833 | 49 | 3568.4404 |
| January, 19 2016 00:00:00 | 6787 | 61045 | 247320 | 60 | 3499.5723 |
| January, 20 2016 00:00:00 | 10875 | 56707 | 254071 | 76 | 3992.852 |
| January, 21 2016 00:00:00 | 8782 | 57286 | 210270 | 70 | 3926.4617 |
| January, 22 2016 00:00:00 | 7215 | 46310 | 202314 | 75 | 3476.776 |
| January, 23 2016 00:00:00 | 5167 | 26890 | 117739 | 49 | 2390.4868 |
| January, 24 2016 00:00:00 | 3729 | 33895 | 176456 | 40 | 1176.4037 |
| January, 25 2016 00:00:00 | 4249 | 32450 | 142695 | 59 | 3491.1138 |
| January, 26 2016 00:00:00 | 5529 | 33609 | 159153 | 58 | 3374.9795 |
| January, 27 2016 00:00:00 | 6328 | 33607 | 160742 | 56 | 3969.424 |
| January, 28 2016 00:00:00 | 6961 | 36075 | 158365 | 61 | 3892.029 |
| January, 29 2016 00:00:00 | 5792 | 37082 | 182249 | 65 | 3426.9507 |
| January, 30 2016 00:00:00 | 5490 | 28900 | 126754 | 41 | 2328.357 |
| January, 31 2016 00:00:00 | 3933 | 23832 | 110352 | 30 | 988.32697 |
但我需要这个:
| date | 2016-01-01 | 2016-01-02 | ... | 2016-01-30 | 2016-01-31 |
|----------|------------|------------|-----|------------|------------|
| sold | 1571 | 7181 | ... | 5490 | 3933 |
| outgoing | 2040 | 6040 | ... | 28900 | 23832 |
| incoming | 79827 | 79827 | ... | 126754 | 110352 |
| forecast | 3568.7388 | 3568.7388 | ... | 2328.357 | 988.32697 |
| sellers | 41 | 31 | ... | 41 | 30 |
在此示例中是否采用了比CASE
更动态的方式:rows to columns mysql