MySQL-UNION ALL查询-未知列错误

时间:2019-06-21 08:56:59

标签: mysql sql

我有以下查询会产生错误#1054

  

错误:ER_BAD_FIELD_ERROR:“字段列表”中的未知列“ unit_name””

    SELECT CONCAT('program:', program_pk) AS global_id,
       program_name AS name,
       NULL AS parent_global_id
FROM program
UNION ALL
SELECT CONCAT('theme:', theme_pk) AS global_id,
       theme_name AS name,
       CONCAT('program:', program_fk) AS parent_global_id
FROM theme 
UNION ALL
SELECT 
       CONCAT('theme:', theme_fk, ',strand:', strand_name) AS global_id,
       strand_name AS name,
       CONCAT('theme:', theme_fk) AS parent_global_id
FROM strand
UNION ALL
SELECT 
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name) AS global_id,
       strandyear_name AS name,
       CONCAT('theme:', theme_fk, ',strand:', strand_name) AS parent_global_id
FROM strandyear sy 
INNER JOIN strand s ON s.strand_pk = sy.strand_fk

UNION ALL
SELECT 
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name) AS global_id,
       unit_name AS name,
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name) AS parent_global_id
FROM unit u 
INNER JOIN strandyear sy ON u.strandyear_fk = sy.strandyear_pk
INNER JOIN strand s ON s.strand_pk = sy.strand_fk

UNION ALL
SELECT 
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name) AS global_id,
       rotation_discipline_block_name AS name,
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name) AS parent_global_id
FROM unit u 
INNER JOIN rotation_discipline_block rdb ON u.unit_pk = rdb.unit_fk
INNER JOIN strandyear sy ON u.strandyear_fk = sy.strandyear_pk
INNER JOIN strand s ON s.strand_pk = sy.strand_fk

UNION ALL
SELECT 
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name, ',learning_event:', learning_event_name) AS global_id,
       learning_event_name AS name,
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name) AS parent_global_id
FROM learning_event le
INNER JOIN rotation_discipline_block rdb ON rdb.rotation_discipline_block_pk = le.rotation_discipline_block_fk
INNER JOIN rotation_discipline_block r ON u.unit_pk = r.unit_fk
INNER JOIN strandyear sy ON u.strandyear_fk = sy.strandyear_pk
INNER JOIN strand s ON s.strand_pk = sy.strand_fk

db-fiddle

问题出在查询的最后INNER JOINS中。您可以在此db-fiddle中看到,省略最后的UNION ALLSELECT很好。解决方法可能很简单,但我看不到。

2 个答案:

答案 0 :(得分:1)

将查询的最后一部分更改为:

UNION ALL
SELECT 
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name, ',learning_event:', learning_event_name) AS global_id,
       learning_event_name AS name,
       CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name) AS parent_global_id
FROM learning_event le
INNER JOIN rotation_discipline_block rdb ON rdb.rotation_discipline_block_pk = le.rotation_discipline_block_fk
INNER JOIN unit u ON u.unit_pk = rdb.unit_fk
INNER JOIN strandyear sy ON u.strandyear_fk = sy.strandyear_pk
INNER JOIN strand s ON s.strand_pk = sy.strand_fk

答案 1 :(得分:1)

在上一次查询中,union表“ rotation_discipline_block”出现了两次。 最后一个查询应该是这样的。

SELECT CONCAT('theme:',
              theme_fk,
              ',strand:',
              strand_name,
              ',strandyear:',
              strandyear_name,
              ',unit:',
              unit_name,
              ',rotation_discipline_block:',
              rotation_discipline_block_name,
              ',learning_event:',
              learning_event_name) AS global_id,
       learning_event_name AS name,
       CONCAT('theme:',
              theme_fk,
              ',strand:',
              strand_name,
              ',strandyear:',
              strandyear_name,
              ',unit:',
              unit_name,
              ',rotation_discipline_block:',
              rotation_discipline_block_name) AS parent_global_id
    FROM learning_event le
 INNER JOIN rotation_discipline_block rdb
    ON rdb.rotation_discipline_block_pk = le.rotation_discipline_block_fk
 INNER JOIN Unit
    ON u.unit_pk = rdb.unit_fk
 INNER JOIN strandyear sy
    ON u.strandyear_fk = sy.strandyear_pk
 INNER JOIN strand s
    ON s.strand_pk = sy.strand_fk