【问题标题】:how to find time difference between two tables column?如何找到两个表列之间的时间差?
【发布时间】:2019-12-12 03:46:50
【问题描述】:

我有两个具有相同列名的表并找到它们之间的区别?

class_time(父表)中的class_time(列名)

0200AM
0230AM
0300AM
0330AM
0400AM
0430AM
0500AM
0530AM

class_period(子表)中的class_time(列名)

0330AM
0200AM:0230AM:0300AM

他们之间的区别就像我想要的那样

0400AM
0430AM
0500AM
0530AM

我试过这个查询,但这个查询只减去 0330AM,因为 0330AM 没有冒号分隔

Select main.Class_time
from (
Select class_time from class_time where class_uid=:P216_CLASS_UID
)main
WHERE NOT EXISTS
(
select distinct  trim(regexp_substr(class_time,'[^:]+', 1, level) ) class_time, level
  from class_period where class_uid=:P216_CLASS_UID 
  and class_time=main.Class_time
   connect by regexp_substr(class_time, '[^:]+', 1, level) is not null
);

【问题讨论】:

  • 我敢打赌这个问题可以说得更清楚。根据上述数据的格式,您似乎在进行某种递归扫描,但是,没有提供任何键或架构可以让任何阅读本文的人以有意义的方式回答您的问题。
  • 你可以分享你迄今为止为解决问题所做的尝试。
  • 请看我尝试过的代码,我已经更新了我的问题
  • edit 您的问题并发布数据库表class_time 和表class_period 的列名称及其数据类型。

标签: sql oracle


【解决方案1】:

通过查看示例值和您的尝试,我假设 class_time(Column) 是 VARCHAR 数据类型。在这种情况下,您可以使用NOT IN 实现此目的。检查以下查询。

SELECT * FROM parent_table 
WHERE  class_time NOT IN (SELECT Regexp_substr(class_time, '[^:]+', 1, LEVEL) AS class_time 
                          FROM   child_table 
                          CONNECT BY Regexp_substr(class_time, '[^:]+', 1, LEVEL) IS NOT NULL) 
ORDER  BY class_time; 

CHECK DEMO HERE

【讨论】:

    【解决方案2】:

    您可以通过以下两个步骤找到上述情况的不同之处:

    1) 创建一个存储过程,将冒号 (:) 分隔的记录分解并存储在临时表中:

    DELIMITER |
    
    CREATE PROCEDURE explode( pDelim VARCHAR(32), pStr TEXT)    BEGIN
      DROP TABLE IF EXISTS temp_explode;
      CREATE TEMPORARY TABLE temp_explode (id INT AUTO_INCREMENT PRIMARY KEY NOT NULL, word VARCHAR(40));
      SET @sql := CONCAT('INSERT INTO temp_explode (word) VALUES (', REPLACE(QUOTE(pStr), pDelim, '\'), (\''), ')');
      PREPARE myStmt FROM @sql;
      EXECUTE myStmt;
    END |
    
    DELIMITER ;
    

    2) 然后从子表中选择分解的上课时间:

    SET @str  = (SELECT GROUP_CONCAT(class_time SEPARATOR ':') FROM class_period); 
    SET @delim = ":"; 
    
    CALL explode(@delim,@str);
    SELECT word FROM temp_explode;
    

    现在您可以在查询中将 class_period 替换为 temp_explode 并将 class_period.class_time 替换为 temp_explode.word

    【讨论】:

    • 这是Mysql方案,而OP需要Oracle方案。
    【解决方案3】:

    如果我理解你的问题,那么你正在寻找减号

    With a as 
    (Select '0200AM' col From dual union all
     Select '0230AM' From dual union all
     Select '0300AM' From dual union all
     Select '0330AM' From dual union all
     Select '0400AM' From dual union all
     Select '0430AM' From dual union all
     Select '0500AM' From dual union all
     Select '0530AM' From dual 
    )
    , b as 
     (Select '0330AM' col From dual union all
      Select '0200AM' From dual union all
      Select '0230AM' From dual union all
      Select '0300AM' From dual
     )
    Select * from a
    minus 
    Select * from b
    

    输出

     COL   
    ------
    0400AM
    0430AM
    0500AM
    0530AM
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-02
      • 2019-11-23
      • 2018-02-21
      • 2016-12-31
      • 2015-03-22
      • 1970-01-01
      • 2012-03-08
      • 2020-08-25
      相关资源
      最近更新 更多