Table from one of the SELECTS cannot be used in field list I'm trying the following query in a MySQL based DB:

SELECT 
    alert.name, alert.new_state_date, alert.prev_state_date
FROM
    grafana.alert
WHERE
    alert.id = 1 
UNION SELECT 
    annotation.epoch
FROM
    grafana.annotation
ORDER BY annotation.id DESC
LIMIT 1;


My intent is to just get the data showing in the same panel so I can stop switching back and forth. However, I get the following error:


  Error Code: 1250. Table 'annotation' from one of the SELECTs cannot be used in field list
To make the two queries compatible for a UNION fill the column list of the second one with NULLs to match the length of that of the first one. To be sure the types of the first column match, you could explicitly cast() them to char. (Optional if they are already of compatible types.)

Also use UNION ALL because UNION (without ALL) tries to eliminate duplicates.

And I guess the ORDER BY was meant for the result FROM grafana.annotatio not for the whole result set. To make MySQL know that, put the second query in parenthesis.

SELECT cast(alert.name AS char),
       alert.new_state_date,
       alert.prev_state_date
       FROM grafana.alert
       WHERE alert.id = 1 
UNION ALL
(SELECT cast(annotation.epoch AS char),
        NULL,
        NULL
        FROM grafana.annotation
        ORDER BY annotation.id DESC
        LIMIT 1);


If both queries return one row, you could also cross join them, to have the results side by side.

SELECT *
       FROM (SELECT alert.name,
                    alert.new_state_date,
                    alert.prev_state_date
                    FROM grafana.alert
                    WHERE alert.id = 1) x
            CROSS JOIN (SELECT annotation.epoch
                               FROM grafana.annotation
                               ORDER BY annotation.id DESC
                               LIMIT 1) y;