mysql-inner与concat连接-字段列表中的“learning\u event\u name”列不明确

gopyfrb3  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(299)

获取错误 Column learning_event_name in field list is ambiguous 使用以下查询:

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, ',learning_event:', learning_event_name) AS global_id,
       learning_event_name AS name,
       CONCAT('year:', year_fk, ',unit:', unit_name) AS parent_global_id
FROM learning_event le
INNER JOIN unit u ON u.unit_pk = le.unit_fk
INNER JOIN year y ON u.year_fk = y.year_pk
INNER JOIN le ON notes_le.learning_event_fk = le.learning_event_pk
WHERE notes_le.user = '$user'

mysql 5.5版
见D小提琴

bhmjp9jg

bhmjp9jg1#

这是你想看的吗?

SELECT 
       CONCAT('year:', year_fk, ',unit:', unit_name, ',learning_event:', le.learning_event_name) AS global_id, # same ambiguous error mentioned by Akina and Strawberry
       le.learning_event_name AS name, # 'le.learning_event_name' this is to fix your 1st error: ambiguous fieldname
       CONCAT('year:', year_fk, ',unit:', unit_name) AS parent_global_id
FROM learning_event le
INNER JOIN unit u ON u.unit_pk = le.unit_fk
INNER JOIN year y ON u.year_fk = y.year_pk
INNER JOIN notes_le ON notes_le.learning_event_fk = le.learning_event_pk # your 2nd error mentioned by Gordon: this is my assumption, you wrote 'INNER JOIN le' but put 'notes_le' on join and where condition
WHERE notes_le.user = '00082563'

正如akina所说,我们不能为你做这件事,因为你是唯一一个知道“学习事件名称”是来自表le、le2,还是笔记le的人。我们不知道,你知道。代码只是我的假设。

相关问题