嗨,我正在寻找以下自反式交叉连接的替代语法。目标是表格的一种行填充器 - 日期应该有每个 CDN 的条目。我正在使用 MySQL
select
d.labelDate,
n.cdn,
networks.sites
from (
select
distinct labelDate
from
cdn_trend
) as d
cross join (
select
distinct cdn
from cdn_trend
) as n
left join cdn_trend as networks
on networks.labelDate = d.labelDate
and networks.cdn = n.cdn
order by
labelDate,
cdn
我已经尝试使用简单的别名来重铸交叉连接,但这给了我连接中的列错误。是否可以这样做或者我应该考虑使用视图来代替?
由于交叉连接应该简单地返回两个表的笛卡尔积,因此它应该与在没有连接的情况下简单地选择两者相同。但是,以下引发了“on 子句中的未知列 d.labelDate”异常
select distinct d.labelDate, n.cdn, networks.sites
from
cdn_trend as d,
cdn_trend as n
left join cdn_trend as networks ON
(n.labelDate = networks.labelDate
and d.cdn = networks.cdn)
order by labelDate, cdn
Error Code: 1054. Unknown column 'd.cdn' in 'on clause'
因为长度d
和n
都比较小,所以查询的大小是足够快的。