简体   繁体   中英

MySQL inner join three tables to use in calendar

Following up my question where I used the answer to generate data on my calendar called maintenance calendar showing the aircraft's maintenance schedule. This is the MySQL query for it:

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."' 

Here is the maintenance_sched database:

maintenance_sched表

And the calendar looks like this (based on the data from maintenance_sched):

在此处输入图片说明

Then, I have another calendar called reservation calendar with the same code as the maintenance calendar though with different query. This is the reservation calendar query: SELECT acode FROM reservation WHERE month(etd) = '".$month."' AND dayofmonth(etd) = '".$dayArray["mday"]."' AND year(etd) = '".$year."' ORDER BY etd" .

The reservation table is this:

在此处输入图片说明

And the reservation calendar looks like this:

在此处输入图片说明

EDIT:

What I want to do is: have these two calendar in one calendar with the result of maintenance_sched query outputted as string with strikethrough . But I can't seem to make the two queries work out together as one.

I do think the answer to this question is to simply join the two queries. An example of this might be like below where you just null out any columns that aren't in your second table.

SELECT id, date, field3, description 
FROM table1 
UNION 
SELECT id, date, field3, null 
FROM table2

As there is no relationship among both the table we cannot go for joins, it would be better to go for UNION to combine the result.

This query uses group_concat so will generate common results in following form

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

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

If you dont want record in this format then just remove group_concat , group by clause from the query.

Query

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;

NOTE For the second query add the according date logic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM