从transactional_detail中选择*;
transaction_id product_id customer_id start_date end_date
1 1 1 30-12-2014 01-01-2015
需要以下面给出的格式输出:
transaction_id product_id customer_id date
1 1 1 30-12-2014
1 1 1 31-12-2014
1 1 1 01-01-2015
请在MYSQL中提供SQL查询。
此致 阿伦
答案 0 :(得分:0)
$startDate = date("d-m-Y",strtotime($row['start_date']));
答案 1 :(得分:0)
为了返回指定的结果集,您需要一个可以连接到表的行源,以生成" duplicate"行。
一种方法是创建并填充日历类型表,其中包含您要返回的所有可能日期的列表:
CREATE TABLE cal (dt DATE NOT NULL PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO cal (dt) VALUES ('2010-01-01');
INSERT INTO cal (dt) SELECT dt + INTERVAL 1 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 2 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 4 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 8 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 16 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 32 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 64 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 128 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 256 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 512 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 1024 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 2048 DAY FROM cal ORDER BY dt;
INSERT INTO cal (dt) SELECT dt + INTERVAL 4096 DAY FROM cal ORDER BY dt;
然后,在连接操作中引用该表,以获得"匹配"的所有dt
值。
假设start_date
和end_date
列是DATE数据类型:
SELECT t.transaction_id
, t.product_id
, t.customer_id
, c.dt
FROM transactional_detail t
JOIN cal c
ON c.dt >= t.start_date
AND c.dt <= t.end_date
ORDER
BY t.transaction_id
, t.product_id
, t.customer_id
, c.dt
如果start_date
和end_date
是VARCHAR而不是DATE(这将是一个非常糟糕的设计选择),您需要将这些转换为DATE数据类型以获取日期比较语义,例如
ON c.dt >= STR_TO_DATE(t.start_date,'%d-%m-%Y')
AND c.dt <= STR_TO_DATE(t.end_date,'%d-%m-%Y')