MySQL-如何修改复杂的内部联接和concat查询?

时间:2019-08-23 02:26:25

标签: mysql sql

我有以下查询,该查询工作正常,但需要创建一个版本,该查询的版本仅返回在查找表learning_event_presentation_lookup上具有匹配项的学习事件,其中presentation_pk = $presentation的查找表包含:

learning_event_fkpresentation_fk

SELECT CONCAT('program:', program_pk) AS global_id,
       program_name AS name,
       NULL AS parent_global_id
FROM program
UNION ALL
SELECT CONCAT('year:', year_pk) AS global_id,
       year_name AS name,
       CONCAT('program:', program_fk) AS parent_global_id
FROM year 
UNION ALL
SELECT 
       CONCAT('year:', year_fk, ',unit:', unit_name) AS global_id,
       unit_name AS name,
       CONCAT('year:', year_fk) AS parent_global_id
FROM unit
UNION ALL
SELECT 
       CONCAT('year:', year_fk, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name) AS global_id,
       rotation_discipline_block_name AS name,
       CONCAT('year:', year_fk, ',unit:', unit_name) AS parent_global_id
FROM rotation_discipline_block rdb
INNER JOIN unit u ON u.unit_pk = rdb.unit_fk
UNION ALL
SELECT 
       CONCAT('year:', year_fk, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name, ',learning_event:', learning_event_name) AS global_id,
       learning_event_name AS name,
       CONCAT('year:', year_fk, ',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 year y ON u.year_fk = y.year_pk
ORDER BY name

我尝试在INNER JOIN之后添加以下内容,但收到错误消息“ where子句中的未知列'learning_event_presentation_lookup.learning_event_fk'”,因为表learning_event_presentation_lookup不在选择查询中。但是我不确定如何在现有查询中添加该表...

WHERE learning_event_presentation_lookup.learning_event_fk = le.learning_event_pk AND learning_event_presentation_lookup.presentation_fk = presentation.presentation_pk

DB fiddle

1 个答案:

答案 0 :(得分:2)

由于这是一堆联合,因此其余查询可以忽略。我们只关心这个:

SELECT 
       CONCAT('year:', year_fk, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name, ',learning_event:', learning_event_name) AS global_id,
       learning_event_name AS name,
       CONCAT('year:', year_fk, ',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 year y ON u.year_fk = y.year_pk
ORDER BY name

我们在learning_event_presentation_lookup上加入learning_event_pk

INNER JOIN learning_event_presentation_lookup lepl ON lepl.learning_event_fk = le.learning_event_pk

现在我们可以将其限制为仅学习与特定演示文稿相关的事件。

WHERE lepl.presentation_fk = :presentation_fk

现在,一起搜索仅与演示文稿#23相关的学习事件。

SELECT 
       CONCAT('year:', year_fk, ',unit:', unit_name, ',rotation_discipline_block:', rotation_discipline_block_name, ',learning_event:', learning_event_name) AS global_id,
       learning_event_name AS name,
       CONCAT('year:', year_fk, ',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 year y ON u.year_fk = y.year_pk
INNER JOIN learning_event_presentation_lookup lepl ON lepl.learning_event_fk = le.learning_event_pk
WHERE lepl.presentation_fk = 23
ORDER BY name

DB Fiddle