FULL JOIN 查询结果错误代码:1054。任何解决方法?
FULL JOIN query results in Error Code: 1054. Any workaround?
我正在尝试连接两个表以显示与 where 子句匹配的所有行,而不仅仅是与连接匹配的行。这是两个帐单表,我正在尝试合并它们,以便两个表中具有匹配年份和月份的每一行都有每个表中的几个字段。
SELECT
tblhydrobill.billyear, tblhydrobill.billmonth, tblhydrobill.KWH as elecconsumption, tblhydrobill.CurrentCharges as eleccost,
tblgasdata.cubicft as gasconsumption, tblgasdata.total as gascost
FROM tblhydrobill
FULL JOIN tblgasdata ON tblhydrobill.billyear = tblgasdata.billyear and tblhydrobill.billmonth = tblgasdata.billmonth
WHERE tblhydrobill.meteridnumber = 19 and tblgasdata.buildingid = 19
ORDER BY tblhydrobill.billyear asc, tblhydrobill.billmonth asc
LEFT JOIN 正确执行,但仅显示两个表上存在 month/year 数据的结果。我正在尝试将其添加到 return 所有行,如果连接的一侧有空字段,如果另一侧没有匹配项,反之亦然。
使用 FULL JOIN 我得到以下 MySQL 错误:
Error Code: 1054. Unknown column 'tblhydrobill.billyear' in 'field list'
更新:
正如@jarlh 所指出的,MySQL 尚不支持 FULL JOIN,谢谢。
由于 MySql 不支持 FULL JOIN
,它使用 FULL
作为 tblhydrobill 的别名。
但是可以模拟 FULL JOIN。
SELECT h.billyear, h.billmonth
, h.KWH as elecconsumption
, h.CurrentCharges as eleccost
, g.cubicft as gasconsumption
, g.total as gascost
FROM tblhydrobill h
LEFT JOIN tblgasdata g
ON g.billyear = h.billyear
AND g.billmonth = h.billmonth
AND g.buildingid = 19
WHERE h.meteridnumber = 19
UNION ALL
SELECT g.billyear, g.billmonth
, h.KWH as elecconsumption
, h.CurrentCharges as eleccost
, g.cubicft as gasconsumption
, g.total as gascost
FROM tblgasdata g
LEFT JOIN tblhydrobill h
ON h.billyear = g.billyear
AND h.billmonth = g.billmonth
AND h.meteridnumber = 19
WHERE g.buildingid = 19
AND h.billyear IS NULL
ORDER BY billyear, billmonth
您可以使用 union
生成一个或两个表中存在的年月对列表,然后将两个表与该结果左连接:
select *
from (
select billyear, billmonth from tblhydrobill where meteridnumber = 19
union
select billyear, billmonth from tblgasdata where buildingid = 19
) as ym
left join tblhydrobill on tblhydrobill.billyear = ym.billyear and tblhydrobill.billmonth = ym.billmonth and tblhydrobill.meteridnumber = 19
left join tblgasdata on tblgasdata.billyear = ym.billyear and tblgasdata.billmonth = ym.billmonth and tblgasdata.buildingid = 19
order by ym.billyear, ym.billmonth
请注意,可以手动构建 ym
列表,例如:
from (
select 2022, 1 union
select 2021, 12 union
select 2021, 11
) as ym
我正在尝试连接两个表以显示与 where 子句匹配的所有行,而不仅仅是与连接匹配的行。这是两个帐单表,我正在尝试合并它们,以便两个表中具有匹配年份和月份的每一行都有每个表中的几个字段。
SELECT
tblhydrobill.billyear, tblhydrobill.billmonth, tblhydrobill.KWH as elecconsumption, tblhydrobill.CurrentCharges as eleccost,
tblgasdata.cubicft as gasconsumption, tblgasdata.total as gascost
FROM tblhydrobill
FULL JOIN tblgasdata ON tblhydrobill.billyear = tblgasdata.billyear and tblhydrobill.billmonth = tblgasdata.billmonth
WHERE tblhydrobill.meteridnumber = 19 and tblgasdata.buildingid = 19
ORDER BY tblhydrobill.billyear asc, tblhydrobill.billmonth asc
LEFT JOIN 正确执行,但仅显示两个表上存在 month/year 数据的结果。我正在尝试将其添加到 return 所有行,如果连接的一侧有空字段,如果另一侧没有匹配项,反之亦然。
使用 FULL JOIN 我得到以下 MySQL 错误:
Error Code: 1054. Unknown column 'tblhydrobill.billyear' in 'field list'
更新:
正如@jarlh 所指出的,MySQL 尚不支持 FULL JOIN,谢谢。
由于 MySql 不支持 FULL JOIN
,它使用 FULL
作为 tblhydrobill 的别名。
但是可以模拟 FULL JOIN。
SELECT h.billyear, h.billmonth
, h.KWH as elecconsumption
, h.CurrentCharges as eleccost
, g.cubicft as gasconsumption
, g.total as gascost
FROM tblhydrobill h
LEFT JOIN tblgasdata g
ON g.billyear = h.billyear
AND g.billmonth = h.billmonth
AND g.buildingid = 19
WHERE h.meteridnumber = 19
UNION ALL
SELECT g.billyear, g.billmonth
, h.KWH as elecconsumption
, h.CurrentCharges as eleccost
, g.cubicft as gasconsumption
, g.total as gascost
FROM tblgasdata g
LEFT JOIN tblhydrobill h
ON h.billyear = g.billyear
AND h.billmonth = g.billmonth
AND h.meteridnumber = 19
WHERE g.buildingid = 19
AND h.billyear IS NULL
ORDER BY billyear, billmonth
您可以使用 union
生成一个或两个表中存在的年月对列表,然后将两个表与该结果左连接:
select *
from (
select billyear, billmonth from tblhydrobill where meteridnumber = 19
union
select billyear, billmonth from tblgasdata where buildingid = 19
) as ym
left join tblhydrobill on tblhydrobill.billyear = ym.billyear and tblhydrobill.billmonth = ym.billmonth and tblhydrobill.meteridnumber = 19
left join tblgasdata on tblgasdata.billyear = ym.billyear and tblgasdata.billmonth = ym.billmonth and tblgasdata.buildingid = 19
order by ym.billyear, ym.billmonth
请注意,可以手动构建 ym
列表,例如:
from (
select 2022, 1 union
select 2021, 12 union
select 2021, 11
) as ym