使用MySQL函数格式化列名称

时间:2012-06-26 19:43:04

标签: mysql sql

我有一个像

这样的查询
select user,
if(purchase_date='2012-06-10' - interval 1 day, revenue, 0),
if(purchase_date='2012-06-10' - interval 2 day, revenue, 0),
if(purchase_date='2012-06-10' - interval 3 day, revenue, 0)
from purchases
group by user;

我希望通过做类似

之类的方式将列的日期格式更改为“Mon 6-10”
select user,
if(purchase_date='2012-06-10' - interval 1 day, revenue, 0) AS date_format('2012-06-10' - interval 1 day, '%M %D-%m')
if(purchase_date='2012-06-10' - interval 2 day, revenue, 0) AS date_format('2012-06-10' - interval 2 day, '%M %D-%m')    
if(purchase_date='2012-06-10' - interval 3 day, revenue, 0) AS date_format('2012-06-10' - interval 3 day, '%M %D-%m')

但我得到一个SQL错误说     错误1064(42000):您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在...'

附近使用正确的语法

是否可以这样做?

我希望输出看起来像

user Mon 6-9 Sun 6-8 Sat 6-7
---- ------- ------- -------
   1      23     34       65
   4      26     21       65
  11      21     65        0

1 个答案:

答案 0 :(得分:2)

你相信我有解决方案吗?

我真的不得不用这个想象力。我稍微更改了查询以每天执行SUM。请跟随:

您基本上需要构建此查询

select user,
SUM(if(purchase_date=DATE(NOW()) - interval 1 day, revenue, 0)) sum1,
SUM(if(purchase_date=DATE(NOW()) - interval 2 day, revenue, 0)) sum2,  
SUM(if(purchase_date=DATE(NOW()) - interval 3 day, revenue, 0)) sum3
from purchases
GROUP BY user;

来自这个

select * from 
(select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
(select distinct purchase_date dt from purchases) dthdrs) DateHeaders;

首先,让我们制作一些样本数据:

drop database if exists idris;
create database idris;
use idris
create table purchases
(
    id int not null auto_increment,
    user int,
    purchase_date date,
    revenue int,
    primary key (id)
) ENGINE=MyISAM;
create table dummydata like purchases;
alter table dummydata drop column id;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set user=4;
insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
delete from dummydata;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set user=11;
insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
delete from dummydata;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set user=1;
insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
delete from dummydata;
select * from purchases;

运行之后,我制作了这些数据:

mysql> select * from purchases;
+----+------+---------------+---------+
| id | user | purchase_date | revenue |
+----+------+---------------+---------+
|  1 |    4 | 2012-06-23    |      45 |
|  2 |    4 | 2012-06-24    |      18 |
|  3 |    4 | 2012-06-25    |      55 |
|  4 |    4 | 2012-06-26    |      20 |
|  5 |   11 | 2012-06-23    |      39 |
|  6 |   11 | 2012-06-24    |      32 |
|  7 |   11 | 2012-06-25    |      44 |
|  8 |   11 | 2012-06-26    |      26 |
|  9 |    1 | 2012-06-23    |      99 |
| 10 |    1 | 2012-06-24    |      17 |
| 11 |    1 | 2012-06-25    |      88 |
| 12 |    1 | 2012-06-26    |      91 |
+----+------+---------------+---------+
12 rows in set (0.00 sec)

mysql>

让我们运行我发布的第一个查询

mysql>  select user,
    -> SUM(if(purchase_date=DATE(NOW()) - interval 1 day, revenue, 0)) sum1,
    -> SUM(if(purchase_date=DATE(NOW()) - interval 2 day, revenue, 0)) sum2,
    -> SUM(if(purchase_date=DATE(NOW()) - interval 3 day, revenue, 0)) sum3
    -> from purchases
    -> GROUP BY user;
+------+------+------+------+
| user | sum1 | sum2 | sum3 |
+------+------+------+------+
|    1 |   88 |   17 |   99 |
|    4 |   55 |   18 |   45 |
|   11 |   44 |   32 |   39 |
+------+------+------+------+
3 rows in set (0.00 sec)

mysql>

这是我发布的第二个查询,它将构建标题

mysql> select * from
    -> (select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
    -> (select distinct purchase_date dt from purchases) dthdrs) DateHeaders;
+------------+----------+
| dt         | dh       |
+------------+----------+
| 2012-06-23 | Sat 6-23 |
| 2012-06-24 | Sun 6-24 |
| 2012-06-25 | Mon 6-25 |
| 2012-06-26 | Tue 6-26 |
+------------+----------+
4 rows in set (0.00 sec)

mysql>

Now let's form VOLTRON

以下是从您需要的标题中合并的两个查询:

mysql> select CONCAT('select user,',rv,' from purchases GROUP BY user')
    -> INTO @voltron from
    -> (select GROUP_CONCAT(CONCAT('SUM(if(purchase_date=''',dt,''',revenue,0)) as "',dh,'"')) rv
    -> from (select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
    -> (select distinct purchase_date dt from purchases) dthdrs) DateHeaders) A;
Query OK, 1 row affected (0.01 sec)
mysql> select @voltron\G
*************************** 1. row ***************************
@voltron: select user,SUM(if(purchase_date='2012-06-23',revenue,0)) as "Sat 6-23",SUM(if(purchase_date='2012-06-24',revenue,0)) as "Sun 6-24",SUM(if(purchase_date='2012-06-25',revenue,0)) as "Mon 6-25",SUM(if(purchase_date='2012-06-26',revenue,0)) as "Tue 6-26" from purchases GROUP BY user
1 row in set (0.00 sec)

mysql>

好的,我形成了查询。但它有效吗?

看哪

mysql> PREPARE s1 FROM @voltron;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE s1;
+------+----------+----------+----------+----------+
| user | Sat 6-23 | Sun 6-24 | Mon 6-25 | Tue 6-26 |
+------+----------+----------+----------+----------+
|    1 |       99 |       17 |       88 |       91 |
|    4 |       45 |       18 |       55 |       20 |
|   11 |       39 |       32 |       44 |       26 |
+------+----------+----------+----------+----------+
3 rows in set (0.00 sec)

mysql> DEALLOCATE PREPARE s1;
Query OK, 0 rows affected (0.00 sec)

mysql>

所以,这是可能的。你只是利用你的想象力让MySQL为你构建查询。