比较两个结果集

时间:2019-02-21 03:46:07

标签: mysql

嗨,我是mysql的新手,我想检查我的t1是否会通过该条件,因为我想检查t1.start1,t1.end1,t1.tool1,t1.mall1是否都在 子查询或子查询t2.start2,t2.end2,t2.tool2,t2.mall2的两个日期start2和end2之间,其中状态未预订或撤消

like my data in t1

                        start1              end1                 tool1            mall1       parentid
                        01/02/2016          01/07/2016           mat1             mallA        id11
                        01/01/2017          01/05/2017            mat4             mallB       id11



data in t2 status are all  not booked or withdrawn

                        start1              end1                 tool1            mall1       parentid
                        01/01/2016          01/08/2016           mat1             mallA        id22
                        01/01/2017         01/08/2017            mat3             mallB        id29

如果我的t1.start1和t1.end1在t2.start2和t2.end2之间或等于mand mall1和tool1相同,它将通过条件

expected output

start1                end1
01/02/2016            01/07/2016  

sql已尝试

SELECT t1.start1,t1.end1
FROM 
    (SELECT p1.start_date as start1, p1.end_date as end1, p1.tool_type_id as tool1, p1.parent_id, p1.mall_id as mall1
                 FROM tbltools p1  JOIN app_fd_iads_reservation b1 ON p1.parent_id = b1.id  WHERE p1.parent_id  = "id11") as t1
LEFT JOIN
    ( SELECT p11.start_date as start2, p11.end_date  AS end2, p11.tool_type_id as tool2, p11.parent_id ,p11.mall_id AS mall2
                 FROM tbltools p11 JOIN tblbooking b11 ON p11.parent_id = b11.id where

 b1.status != 'Booked' && b1.status != 'Withdrawn' ) 

                 ) as t2
ON ( 
                         (t1.start1 = t2.start2 OR  t1.end1 = t2.end2) 
                         OR (t1.start1> t2.start2 AND t1.end1 < t2.end2) 
                         OR (t1.start1> t2.start2 AND t1.start1<  t2.start2 ) 
                         OR (t1.end1 > t2.end2 AND t1.end1 < t2.end2) 
                         OR (t1.start1 < t2.start2  AND t1.end1 > t2.end2) )
                         AND t1.tool1 = t2.tool2
                         and t1.mall1 =t1.mall2

1 个答案:

答案 0 :(得分:0)

假设table1的起始日期和结束日期应介于table2的起始日期和结束日期之间,下面的查询将起作用

SELECT    t1.start1, 
          t1.end1 
FROM      ( 
                 SELECT p1.start_date   AS start1, 
                        p1.end_date     AS end1, 
                        p1.tool_type_id AS tool1, 
                        p1.parent_id, 
                        p1.mall_id AS mall1 
                 FROM   tbltools p1 
                 JOIN   app_fd_iads_reservation b1 
                 ON     p1.parent_id = b1.id 
                 WHERE  p1.parent_id = "id11") AS t1 
LEFT JOIN 
          ( 
                 SELECT p11.start_date   AS start2, 
                        p11.end_date     AS end2, 
                        p11.tool_type_id AS tool2, 
                        p11.parent_id , 
                        p11.mall_id AS mall2 
                 FROM   tbltools p11 
                 JOIN   tblbooking b11 
                 ON     p11.parent_id = b11.id 
                 WHERE  b1.status != 'Booked' && b1.status != 'Withdrawn' ) ) AS t2 ON ( 
  AND 
  t1.tool1 = t2.tool2 
  AND 
  t1.mall1 =t1.mall2 
) 
WHERE t1.start1 BETWEEN t2.start2 
AND 
t2.end2 
AND 
t1.end1 BETWEEN t2.start2 
AND 
t2.end2