SQL转换和计算存储为字符串的日期之间的差异 - MySQL

时间:2017-06-02 16:22:07

标签: mysql sql pivot mariadb

我有两张桌子。第一个称为posts,第二个称为postmeta。 (如果有人注意到,我正在使用Wordpress数据库,这对于完成此任务并不重要。)

posts表看起来像这样(为了缩短目的)。

ID  |  post_title | post_status | post_type
------------------------------------------
1   |  One        | publish     | hours
2   |  Two        | publish     | hours

postmeta表看起来像这样。日期格式为d.m.Y. G:我:S。

meta_id | post_id | meta_key | meta_value
------------------------------------------
1       | 1       | from     | 1.1.2017. 10:00:00
2       | 1       | to       | 1.1.2017. 16:00:00
3       | 2       | from     | 2.1.2017. 12:00:00
4       | 2       | to       | 2.1.2017. 15:00:00

在这些表ID = post_id中。 想要的结果是下面的表格,date_difffromto之间的差异,以小时为单位,必须由SQL计算(date_diff = to - from)。请注意,meta_key定义为VARCHARmeta_value定义为LONGTEXT,这会使计算更加困难。

ID | title | from               | to                 | date_diff
------------------------------------------------------------------
1  | 1     | 1.1.2017. 10:00:00 | 1.1.2017. 16:00:00 | 6
2  | 1     | 2.1.2017. 12:00:00 | 2.1.2017. 15:00:00 | 3

这是我现在的代码。使行成为列对我来说有点问题,而且计算更多。

SELECT posts.ID, posts.post_title, postmeta.meta_key, postmeta.meta_value 
FROM posts
INNER JOIN postmeta 
ON posts.ID = postmeta.post_id
WHERE post_status = 'publish' 
AND post_type = 'hours' 
AND (postmeta.meta_key = 'from' OR postmeta.meta_key = 'to');

非常感谢。 :)

4 个答案:

答案 0 :(得分:1)

您可以在postmeta上使用自联接来获取两个分隔的列 使用str_to_date在日期中转换字符串 使用适当的背景来保留单词(例如:from) 和TIMESTAMPDIFF获取差异的HOURS

  SELECT 
          posts.ID
        , posts.post_title
        , str_to_date(table_from.meta_value, '%d.%m.%Y. %H:%i:%s')  as `from` 
        , str_to_date(table_to.meta_value, '%d.%m.%Y. %H:%i:%s' ) as `to` 
        , TIMESTAMPDIFF(HOUR, str_to_date(table_from.meta_value, '%d.%m.%Y. %H:%i:%s') , 
                                    str_to_date(table_to.meta_value, '%d.%m.%Y. %H:%i:%s' )) as diff
  FROM posts
  INNER JOIN postmeta table_from ON posts.ID = table_from.post_id and table_from.meta_key ='from'
  inner join postmeta table_to ON posts.ID = table_to.post_id and table_to.meta_key ='to'
  WHERE post_status = 'publish' 
  AND post_type = 'hours' 

表示小数,可以使用

timestampdiff(MINUTE,startdate,enddate)/ 60 as diff

答案 1 :(得分:1)

E.g。 (比Scaisedge的方法慢,但更容易阅读......)

DROP TABLE IF EXISTS posts;

CREATE TABLE posts
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,post_title VARCHAR(12) NOT NULL
,post_status VARCHAR(12) NOT NULL
,post_type VARCHAR(12) NOT NULL
);

INSERT INTO posts VALUES
(1,'One','publish','hours'),
(2,'Two','publish','hours');

DROP TABLE IF EXISTS postmeta;

CREATE TABLE postmeta
(meta_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,post_id INT NOT NULL
,meta_key VARCHAR(12) NOT NULL
,meta_value VARCHAR(20) NOT NULL
);

INSERT INTO postmeta VALUES
(1,1,'from','1.1.2017. 10:00:00'),
(2,1,'to'  ,'1.1.2017. 16:00:00'),
(3,2,'from','2.1.2017. 12:00:00'),
(4,2,'to'  ,'2.1.2017. 15:00:00');

-- Date format is d.m.Y. H:i:s.

SELECT post_id
     , MAX(CASE WHEN meta_key = 'from' THEN STR_TO_DATE(meta_value,'%d.%m.%Y. %H:%i:%s') END) `from`
     , MAX(CASE WHEN meta_key = 'to' THEN STR_TO_DATE(meta_value,'%d.%m.%Y. %H:%i:%s') END) `to`
     , TIMEDIFF(
           MAX(CASE WHEN meta_key = 'to' THEN STR_TO_DATE(meta_value,'%d.%m.%Y. %H:%i:%s') END)
         , MAX(CASE WHEN meta_key = 'from' THEN STR_TO_DATE(meta_value,'%d.%m.%Y. %H:%i:%s') END)
               ) date_diff
  FROM postmeta 
 GROUP 
    BY post_id;

+---------+---------------------+---------------------+-----------+
| post_id | from                | to                  | date_diff |
+---------+---------------------+---------------------+-----------+
|       1 | 2017-01-01 10:00:00 | 2017-01-01 16:00:00 | 06:00:00  |
|       2 | 2017-01-02 12:00:00 | 2017-01-02 15:00:00 | 03:00:00  |
+---------+---------------------+---------------------+-----------+

我已将最后一部分作为(简单)练习留给读者。

答案 2 :(得分:0)

我希望它会有所帮助。

SELECT 
   p.id, 
   p.post_title, 
   postmeta_temp.from, 
   postmeta_temp.to, 
   postmeta_temp.date_diff 
FROM   posts p 
   INNER JOIN (SELECT postmeta_from.post_id 
                      AS post_id, 
                      postmeta_from.meta_key 
                                     AS from, 
                      postmeta_to.meta_key 
                                     AS to, 
                      Date_diff(postmeta_to.meta_value, 
                      postmeta_from.meta_value) 
                                     AS date_diff 
               FROM   postmeta postmeta_from 

                      INNER JOIN postmeta postmeta_to 

                              ON postmeta_from.post_id = postmeta_to.post_id 

                                 AND postmeta_from.meta_key = 'from' 

                                 AND postmeta_to.meta_key = 'to') AS 

              postmeta_temp 

           ON p.id = postmeta_temp.post_id 

答案 3 :(得分:0)

我会在下次使用相同的ID和下一个日期检查每个ID。

select pm.post_id as ID, pm.post_id as title, pm.metavalue as from,  
(select x.metavalue from postmeta x where x.enter code herepost_id = pm.post_id and not x.meta_key = 'from') 
as to , 
(select cast(x.metavalue as timestamp) - cast(pm.metavalue as timestamp) from postmeta x 
      where x.post_id = pm.post_id and not x.meta_key = 'from') as date_diff from 
postmeta pm where pm.meta_key = 'from';


 id | title |        from        |         to         | date_diff 
----+-------+--------------------+--------------------+-----------
  1 |     1 | 1.1.2017. 10:00:00 | 1.1.2017. 16:00:00 | 06:00:00
  2 |     2 | 2.1.2017. 12:00:00 | 2.1.2017. 15:00:00 | 03:00:00