0

跟进我的问题,我使用答案在我的日历上生成数据,称为维护日历,显示飞机的维护计划。这是它的 MySQL 查询:

SELECT DISTINCT s.reg  AS 'reg', 
                a.date AS 'date' 
FROM   (SELECT Curdate() 
               + INTERVAL (a.a + (10 * b.a) + (100 * c.a)) day AS Date 
        FROM   (SELECT 0 AS a 
                UNION ALL SELECT 1 
                UNION ALL SELECT 2 
                UNION ALL SELECT 3 
                UNION ALL SELECT 4 
                UNION ALL SELECT 5 
                UNION ALL SELECT 6 
                UNION ALL SELECT 7 
                UNION ALL SELECT 8 
                UNION ALL SELECT 9) AS a 
               CROSS JOIN (SELECT 0 AS a 
                           UNION ALL SELECT 1 
                           UNION ALL SELECT 2 
                           UNION ALL SELECT 3 
                           UNION ALL SELECT 4 
                           UNION ALL SELECT 5 
                           UNION ALL SELECT 6 
                           UNION ALL SELECT 7 
                           UNION ALL SELECT 8 
                           UNION ALL SELECT 9) AS b 
               CROSS JOIN (SELECT 0 AS a 
                           UNION ALL SELECT 1 
                           UNION ALL SELECT 2 
                           UNION ALL SELECT 3 
                           UNION ALL SELECT 4 
                           UNION ALL SELECT 5 
                           UNION ALL SELECT 6 
                           UNION ALL SELECT 7 
                           UNION ALL SELECT 8 
                           UNION ALL SELECT 9) AS c) a 
       INNER JOIN maintenance_sched s 
               ON a.date >= s.date_from 
                  AND a.date <= s.date_to 
WHERE  Month(date) = '".$month."' 
       AND Dayofmonth(date) = '".$dayArray["mday"]."' 
       AND Year(date) = '".$year."' 

这是 maintenance_sched 数据库:

维护计划表

日历看起来像这样(基于maintenance_sched 的数据):

在此处输入图像描述

然后,我有另一个名为预订日历的日历,其代码与维护日历相同,但查询不同。这是预订日历查询:SELECT acode FROM reservation WHERE month(etd) = '".$month."' AND dayofmonth(etd) = '".$dayArray["mday"]."' AND year(etd) = '".$year."' ORDER BY etd"

预约表是这样的:

在此处输入图像描述

预订日历如下所示:

在此处输入图像描述

编辑:

我想要做的是:将这两个日历放在一个日历中,并将 maintenance_sched 查询的结果输出为带有删除线的字符串。但我似乎无法将这两个查询合二为一。

4

2 回答 2

0

由于两个表之间没有关系,我们不能进行连接,最好使用 UNION 来合并结果。

此查询使用 group_concat 因此将生成以下形式的常见结果

2013-03-15 | RP-C1728, RP-C1086

2013-03-08 | RP-C1728, RP-C1086, RP-C143

如果您不想以这种格式记录,那么只需group_concat从查询中删除 , group by 子句。

询问

SELECT a.date, group_concat(a.reg) 
FROM 
     (SELECT DISTINCT s.reg  AS 'reg', 
                a.date AS 'date' 
        FROM   (SELECT Curdate() 
               + INTERVAL (a.a + (10 * b.a) + (100 * c.a)) day AS Date 
            FROM   (SELECT 0 AS a 
                UNION ALL SELECT 1 
                UNION ALL SELECT 2 
                UNION ALL SELECT 3 
                UNION ALL SELECT 4 
                UNION ALL SELECT 5 
                UNION ALL SELECT 6 
                UNION ALL SELECT 7 
                UNION ALL SELECT 8 
                UNION ALL SELECT 9) AS a 
               CROSS JOIN (SELECT 0 AS a 
                           UNION ALL SELECT 1 
                           UNION ALL SELECT 2 
                           UNION ALL SELECT 3 
                           UNION ALL SELECT 4 
                           UNION ALL SELECT 5 
                           UNION ALL SELECT 6 
                           UNION ALL SELECT 7 
                           UNION ALL SELECT 8 
                           UNION ALL SELECT 9) AS b 
               CROSS JOIN (SELECT 0 AS a 
                           UNION ALL SELECT 1 
                           UNION ALL SELECT 2 
                           UNION ALL SELECT 3 
                           UNION ALL SELECT 4 
                           UNION ALL SELECT 5 
                           UNION ALL SELECT 6 
                           UNION ALL SELECT 7 
                           UNION ALL SELECT 8 
                           UNION ALL SELECT 9) AS c) a 
       INNER JOIN maintenance_sched s 
               ON a.date >= s.date_from 
                  AND a.date <= s.date_to 
WHERE  Month(date) = '".$month."' 
       AND Dayofmonth(date) = '".$dayArray["mday"]."' 
       AND Year(date) = '".$year."' 
UNION ALL 
SELECT acode as 'reg', date as 'date' //Add the date logic here as per your need
FROM reservation 
WHERE month(etd) = '".$month."' AND 
dayofmonth(etd) = '".$dayArray["mday"]."' AND 
year(etd) = '".$year."' ORDER BY etd) a
GROUP BY a.date;

注意对于第二个查询添加相应的日期逻辑

于 2013-03-12T05:38:46.737 回答
0

我确实认为这个问题的答案是简单地加入这两个查询。这方面的一个示例可能如下所示,您只需将不在第二个表中的任何列清空。

SELECT id, date, field3, description 
FROM table1 
UNION 
SELECT id, date, field3, null 
FROM table2
于 2013-03-12T04:24:40.833 回答