【问题标题】:MySQL aliases for select within select in full outer join在完全外连接中选择内选择的 MySQL 别名
【发布时间】:2019-05-15 12:12:17
【问题描述】:

我正在使用来自How to do a FULL OUTER JOIN in MySQL? 的答案在 MySQL 5.7 中进行完全外部联接。

但是,我不确定如何为子表添加别名,以便查询使用与左连接相同的表进行右连接(甚至不确定 MySQL 是否会在后台优化它) .

代码如下:

select *

from

(select cell as hdi_social_cell, count(*) as hdi_social_cell_count
from Detector_Social_Events where streamtype='social' and source = 'hdi'
group by cell having hdi_social_cell_count > 5) social_hdi

left join

(select cell as ml_social_cell, count(*)*0.1 as ml_social_cell_count
from Detector_Social_Events where streamtype='social' and source = 'ml'
group by cell having ml_social_cell_count > 1) social_ml

on social_hdi.cell = social_ml.cell

union select * from

(select cell as hdi_social_cell, count(*) as hdi_social_cell_count
from Detector_Social_Events where streamtype='social' and source = 'hdi'
group by cell having hdi_social_cell_count > 5)  social_hdi 

right join 

(select cell as ml_social_cell, count(*)*0.1 as ml_social_cell_count
from Detector_Social_Events where streamtype='social' and source = 'ml'
group by cell having ml_social_cell_count > 1)  social_ml

on social_hdi.cell = social_ml.cell;

查询返回错误:

ERROR 1054 (42S22): Unknown column 'social_hdi.cell' in 'on clause'

【问题讨论】:

  • 你的意思是错误是'social_hdi.cell' in 'on clause' 吗?我在任何地方都没有看到twitter_hdi
  • 是的,你是对的。我已修复错误消息。

标签: mysql sql


【解决方案1】:

因为你查询select cell as hdi_social_cell

所以你必须编码

on social_hdi.hdi_social_cell = social_ml.hdi_social_cell

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 2011-12-22
    相关资源
    最近更新 更多