MariaDB 10.4 不能多次使用 cte
MariaDB 10.4 Can not use a cte more than once
这是您可以使用的代码 运行,
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=74d081784b301ddfa8bf6d361c586693
with date_ranges(`start`, `end`, title) as (
select '2021-11-22', '2021-12-21 23:59:59', '1400-09'
union all
select '2021-12-22', '2022-01-20 23:59:59', '1400-10'
union all
select '2022-02-21', '2022-03-19 23:59:59', '1400-11'
),
income as (
select count(*) as input,
dr.title as month
from date_ranges dr
#join drivers u on u.created_at between dr.start and dr.end
group by dr.title
),
outcome as (
select count(*) as input,
ddr.title as month
from date_ranges ddr
#join drivers u on u.created_at between ddr.start and ddr.end
group by ddr.title
)
select input, month
from income i
order by month desc
上面的 link 运行良好,但我的本地数据库 MariaDB 10.4 报告错误
Unknown column 'ddr.title' in 'field list'
这是一个已知错误,已在最新版本的 MariaDB 中修复。
以下 fiddle 显示 10.4.22-MariaDB 未出现此问题,但我已验证 10.5.0-MariaDB-log 确实会产生错误,而 10.3.32-MariaDB、10.4 .22-MariaDB、10.5.13-MariaDB、10.6.5-MariaDB没有这个问题。
要解决此错误,我们可以在报告未知列的常用 table 表达式项的 select 列表中添加一个显式别名(派生列名称)。
注意,对于 date_ranges
CTE 术语中第一个 UNION 术语的第 3 个 select 列表项,我通过 AS title
提供了派生列名称。这避免了错误。我们可以对报告为未知的任何类似列执行此操作。
调整后SQL:
with date_ranges(`start`, `end`, title) as (
select '2021-11-22', '2021-12-21 23:59:59', '1400-09' AS title
union all
select '2021-12-22', '2022-01-20 23:59:59', '1400-10'
union all
select '2022-02-21', '2022-03-19 23:59:59', '1400-11'
),
income as (
select count(*) as input,
dr.title as month
from date_ranges dr
#join drivers u on u.created_at between dr.start and dr.end
group by dr.title
),
outcome as (
select count(*) as input,
ddr.title as month
from date_ranges ddr
#join drivers u on u.created_at between ddr.start and ddr.end
group by ddr.title
)
select input, month
from income i
order by month desc
;
这是您可以使用的代码 运行,
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=74d081784b301ddfa8bf6d361c586693
with date_ranges(`start`, `end`, title) as (
select '2021-11-22', '2021-12-21 23:59:59', '1400-09'
union all
select '2021-12-22', '2022-01-20 23:59:59', '1400-10'
union all
select '2022-02-21', '2022-03-19 23:59:59', '1400-11'
),
income as (
select count(*) as input,
dr.title as month
from date_ranges dr
#join drivers u on u.created_at between dr.start and dr.end
group by dr.title
),
outcome as (
select count(*) as input,
ddr.title as month
from date_ranges ddr
#join drivers u on u.created_at between ddr.start and ddr.end
group by ddr.title
)
select input, month
from income i
order by month desc
上面的 link 运行良好,但我的本地数据库 MariaDB 10.4 报告错误
Unknown column 'ddr.title' in 'field list'
这是一个已知错误,已在最新版本的 MariaDB 中修复。
以下 fiddle 显示 10.4.22-MariaDB 未出现此问题,但我已验证 10.5.0-MariaDB-log 确实会产生错误,而 10.3.32-MariaDB、10.4 .22-MariaDB、10.5.13-MariaDB、10.6.5-MariaDB没有这个问题。
要解决此错误,我们可以在报告未知列的常用 table 表达式项的 select 列表中添加一个显式别名(派生列名称)。
注意,对于 date_ranges
CTE 术语中第一个 UNION 术语的第 3 个 select 列表项,我通过 AS title
提供了派生列名称。这避免了错误。我们可以对报告为未知的任何类似列执行此操作。
调整后SQL:
with date_ranges(`start`, `end`, title) as (
select '2021-11-22', '2021-12-21 23:59:59', '1400-09' AS title
union all
select '2021-12-22', '2022-01-20 23:59:59', '1400-10'
union all
select '2022-02-21', '2022-03-19 23:59:59', '1400-11'
),
income as (
select count(*) as input,
dr.title as month
from date_ranges dr
#join drivers u on u.created_at between dr.start and dr.end
group by dr.title
),
outcome as (
select count(*) as input,
ddr.title as month
from date_ranges ddr
#join drivers u on u.created_at between ddr.start and ddr.end
group by ddr.title
)
select input, month
from income i
order by month desc
;